This preview of pull request 1639 is meant for internal use only.

Set up a custom domain proxy for Analytics.js


Custom domains allow you to proxy Analytics.js and proxy all tracking event requests through your domain.

Prerequisites

To set up a custom domain, you need:

  1. Access to your site DNS settings
  2. A CDN you can serve assets from
  3. Access to the CDN settings

This guide explains how to set up a custom domain in CloudFront. The same principles can be applied to almost any modern CDN that supports proxies.

You need to set up two important parts, regardless of the CDN provider you use:

  • Proxy to Segment CDN (cdn.segment.com)
  • Proxy to Segment tracking API (api.segment.io)

Set up

Follow the directions listed for CloudFront or use your own CDN setup. Once you complete those steps and verify that your proxy works for both cdn.segment.com and api.segment.io, contact Segment Product Support with the following template email:

Hi,

This is {person} from {company}. I would like to configure a proxy for the following source(s) to point to the corresponding proxy url:

* Source {link to source in Segment} with source ID {source id} should point to {api host}
* Source {link to source in Segment} with source ID {source id} should point to {api host}

Double-check the source link, the Source ID, and the API proxy host to make sure they are correct.

A Segment Customer Success team member will respond that they have enabled this option for your account. When you receive this confirmation, go to your workspace, then navigate to your source settings, go to Analytics.js, and modify the “Host Address” setting. Change it from api.segment.io/v1 to [your proxy host]/v1.

CloudFront

CDN Proxy

First log in to AWS and navigate to CloudFront.

Click Create Distribution.

Select a delivery method for your content. Click Get Started in the Web section.

Next, configure the distribution settings. Under Origin Settings, update the following values:

Field Value Description
Origin Domain Name cdn.segment.com The domain name you want the proxy to be served to
Origin ID (optional) The Segment CDN A Description of the origin. This can be anything you want that describes the origin domain name.
Origin Protocol Policy Set to HTTPS Only
Alternate Domain Names (CNAMEs) yourdomain.com Add the domain you are proxying to Segment here. If you don’t do this you will get an error.

Next, configure the Default Cache Behavior Settings.

Field Value Description
Viewer Protocol Policy Redirect HTTP to HTTPS Ensure that all traffic goes through HTTPS
Allowed HTTP Methods GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE Select which HTTP methods are allowed to be proxied.
Forward Cookies All Ensure that cookies are forwarded.
Query String Forwarding and Caching Forward all, cache based on all Ensure that all query string values are forwarded and properly cached.

Click Create Distribution at the bottom of the page. This distribution displays as being “In Progress” until it finishes deploying.

You need the “Domain Name” for the next step, so keep this browser window open.

Add CNAME Record to DNS

Next, add a CNAME record to your DNS settings. Go to your domain registrar and add a new record to your DNS of type “CNAME”.

Field Value
Name {subdomain_name}.yourdomain.com
Value CloudFront Distribution Domain Name

Save your record. This might take some time to take effect, depending on your TTL settings. Try curling your domain to check if it is proxying correctly.

Tracking API Proxy

Next, set up a proxy for the tracking API so that all calls are proxied through your domain. In this step, we set up a CloudFront distribution that’s very similar to the previous step, with a few minor changes:

Field Value Description
Origin Domain Name api.segment.io The domain name you would like the proxy to be served to

Add CNAME Record to DNS

Next, add a CNAME record to your DNS settings. Go to your domain registrar and add a new record to your DNS of type “CNAME”. This time use the CloudFront distribution for the tracking API proxy.

Field Value
Name {subdomain_name}.yourdomain.com
Value Tracking API CloudFront Distribution Domain Name

Save your record. This might take some time to take effect, depending on your TTL settings. Try running curl on your domain to check if the proxy is working correctly.

This page was last modified: 01 Dec 2020



Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
or
Create free account