Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM
Accelerate development with AI

On this page
Looking for more inspiration?Visit the

Gtag Destination


(information)

Info

The Gtag Destination is in a closed Early Access Preview. To join the preview, contact Segment Support(link takes you to an external page) or your CSM. The use is governed by (1) Segment First Access(link takes you to an external page) and Beta Terms and Conditions and (2) Segment Acceptable Use Policy(link takes you to an external page).


Getting started

getting-started page anchor

The Google Site Tag (Gtag) Device Mode destination allows you to unify tracking across the Google Suite using the gtag.js library. The Gtag destination supports sending data to Google Analytics. To enable, add your Google Measurement ID into the destination settings. You can either choose to use a Web Measurement Id or a Web + App Measurement Id.


When you make a Page call, Segment sends a pageview to Google Analytics. Pageviews send from the browser.

The resulting page event name in Google Analytics corresponds to the fullName of your page event. The fullName is a combination of the category and name parameters. For example, analytics.page('Home'); produces a Home Page event in GA's dashboard, but analytics.page('Retail Page', 'Home'); produces an event called Retail Page Home.

Virtual pageviews

virtual-pageviews page anchor

Virtual pageviews are when you send a pageview to Google Analytics when the page URL didn't actually change. You can do this through Segment by making a Page call with optional properties, as in the example below.

1
analytics.page({
2
title: 'Signup Modal',
3
url: 'https://segment.com/#signup',
4
path: '/#signup',
5
referrer: 'https://segment.com/'
6
});

By default Segment sends the domain and path to Google Analytics. For example, if someone views the page at http://domain.com/page/?xyz=123&r=5, Segment sends http://domain.com/page/ URL to Google Analytics.

In some cases, like using Google Analytics to track search queries, you might want to pass the whole URL including a query string to Google Analytics. To enable this, go to the Gtag destination settings, and check the Include the Query String option in the Advanced Options.


Segment recommends Analytics.js, the device-mode JavaScript library, for collecting UTM parameter data because Analytics.js collects this data automatically.

Pass UTM parameters in the context object in context.campaign. For Google Analytics, send campaign.name, campaign.source and campaign.medium all together to ensure they appear in reports. The other two parameters (campaign.term and campaign.content) are both optional, but are forwarded to Google Analytics if you send them to Segment.


Google's terms of service forbid passing Personally Identifiable Information (PII) to your Google Analytics reporting interface. For that reason Segment does not pass data from an Identify call to Google unless you specifically request it. You can read about Google's best practices for avoiding this in the Best practices to avoid sending Personally Identifiable Information (PII)(link takes you to an external page) documentation.

Google Analytics Universal tracking method allows you to set a user ID for your identified visitors. Read more here(link takes you to an external page).

To use this feature you must enable User-ID in your Google Analytics property and create a User-ID view, read more here(link takes you to an external page).

To pass the id from your Identify calls to the Gtag destination, go to Other Settings and set Send User-ID to GA to "on" to enable this setting.

The example below represents Segment sending a User-ID value in an identify call:

1
analytics.identify('12345', {
2
email: 'example@example.com',
3
name: 'Jake Peterson'
4
});

In this example the User-ID is 12345 for Google Analytics, but Segment won't share the email or name traits with Google.

(warning)

Warning

If you pass an email, phone number, full name or other PII as the id in Identify do not use this feature. That is against the Google Analytics terms of service and Google may suspend your service.

Google Analytics has several scopes for each custom dimension:

  • hit (synonymous with events)
  • session
  • user
  • product (requires enabling enhanced ecommerce).

Segment's device-mode Analytics.js library supports them all.

Configuring custom dimensions

configuring-custom-dimensions page anchor

To configure a custom dimension:

  1. Configure the Custom Dimensions in your Google Analytics admin page. For more information about creating custom dimensions in Google Analytics, see the Google support article [UA] Create and edit custom dimensions and metrics(link takes you to an external page).

  2. After you've enabled Google Analytics in Segment, you can map traits and properties to your custom dimensions.

  3. From your Segment Workspace, open the destinations catalog and select the Gtag destination, then Settings. Locate Custom Dimensions and declare the mapping.

    For example, to map a Gender property to dimension 1 and User Type to dimension 2, make the following mappings in both Segment and Google Analytics:

    In Segment:

    Google Custom Dimensions with Gender mapped to index 1 and User Type mapped to index 2.

    In Google:

    A screenshot of the Google Custom Dimensions page, with Gender mapped to index 1 and User Type mapped to dimension 2.
(success)

Success!

You can map traits and properties to one Custom Dimension in Google Analytics.

After you map your dimensions, Segment checks the user traits and properties in Identify, Track and Page calls to see if you defined them as a dimension. If you have defined them in your mapping, Segment sends that dimension to Google Analytics.

(success)

Success!

Segment sends traits in Identify calls that map to Custom Dimensions in Google Analytics when the next Track or Page call triggers from the browser.

Continuing the example above, you can set the Gender trait with the value of Male, which maps to dimension 1. Segment passes this value to Google Analytics with Viewed History Track calls.

1
analytics.identify({
2
Gender: 'Male'
3
});
4
analytics.track('Viewed History');

Segment records a Google Analytics event whenever you make a Track call. You can see your events inside Google Analytics at Behavior -> Events -> Overview. Keep reading for more details about the Google Analytics event category, action, label, value and how to populate them.

Segment sends events from the browser. For example, a Track call that captures a Logged In event looks like this:

analytics.track('Logged In');

Segment sends these event attributes to Google Analytics:

Event AttributesInfo
Event CategoryAll
Event ActionLogged In

In this example, Segment sends all Google Analytics event parameters:

1
{
2
"userId": "12345",
3
"action": "track",
4
"event": "Logged In",
5
"properties": {
6
"category": "Account",
7
"label": "Premium",
8
"value": 50
9
}
10
}

Segment sends these event attributes to Google Analytics:

Event AttributesInfo
Event CategoryAccount
Event ActionLogged In
Event LabelPremium
Event Value50

To create an event with the nonInteraction flag pass an event property labeled nonInteraction with the value of 1. Alternatively, you can set events as non-interactive by default in the destination settings.

For example:

1
{
2
"action": "track",
3
"event": "Viewed Legal Info",
4
"properties": {
5
"nonInteraction": 1
6
}
7
}

Segment supports Google Analytics basic ecommerce tracking across all libraries. All you have to do is adhere to the Ecommerce tracking API and Segment sends the appropriate data to Google Analytics.

Segment recommends that you include all Ecommerce events. Order Completed is the required event in this call. For each order completed you must include an orderId, and for each product inside that order, you must include an id and name for each product. All other properties are optional.

The most important thing to remember in Google's Universal Analytics is to enable Ecommerce tracking on the view you want to use to track transactions.

Without this step transactions will not show up in your reports.


Segment supports Google Analytics Enhanced Ecommerce tracking in the Gtag destination. Enhanced Ecommerce allows you to derive insights by combining impression data, product data, promotion data, and action data. Product-scoped custom dimensions require Enhanced Ecommerce tracking.

To get started, enable Enhanced Ecommerce and adhere to the standard Ecommerce tracking API. Segment sends necessary data to Google Analytics with the Google Analytics enhanced ecommerce API

To see Enhanced Ecommerce data in your reports, you must be using Google Analytics Universal and enable Enhanced Ecommerce in your Google Analytics view.

Required steps (enhanced)

required-steps-enhanced page anchor

Like standard ecommerce, Order Completed is the required event. This call must also include an orderId and an array of products, each containing an id or name.

For all events that include product details you must pass either name or product_id. For product_id Segment defaults to properties.product_id and fallback to properties.sku.

All other properties are optional. The Refunded Order event also requires an orderId.

Measuring checkout steps

measuring-checkout-steps page anchor

To take full advantage of all the features of Enhanced Ecommerce, you'll want to take advantage of some specific events. The biggest differentiator between ecommerce and Enhanced Ecommerce is support for checkout steps. To take advantage of tracking your checkout funnel and measuring metrics like cart abandonment, you'll first need to configure your checkout funnel in the Google Analytics admin interface, giving readable labels to the numeric checkout steps:

Google Ecommerce setup with four checkout steps: Review Cart, Collect Payment Info, Confirm Purchase, Receipt.

Then you'll instrument your checkout flow with Viewed Checkout Step and Completed Checkout Step events for each step of the funnel you configured in the Google Analytics admin interface, passing the step number and step-specific options through as a property of those events:

1
//upon arrival at first checkout step ('Review Cart' per the screenshot example above)
2
analytics.track('Viewed Checkout Step', {
3
step: 1
4
});
5
6
//upon completion of first checkout step ('Review Cart')
7
analytics.track('Completed Checkout Step', {
8
step: 1
9
});
10
11
//upon arrival at second checkout step ('Collect Payment Info' per the screenshot example above)
12
analytics.track('Viewed Checkout Step', {
13
step: 2
14
});
15
16
//upon completion of this checkout step ('Collect Payment Info')
17
analytics.track('Completed Checkout Step', {
18
step: 2,
19
//if this is the shipping step
20
shippingMethod: 'FedEx',
21
//if this is the payment step
22
paymentMethod: 'Visa'
23
});
24
25
//upon arrival at third checkout step ('Confirm Purchase Details' per the screenshot example above)
26
analytics.track('Viewed Checkout Step', {
27
step: 3
28
});
29
30
//upon completion of third checkout step ('Confirm Purchase Details')
31
analytics.track('Completed Checkout Step', {
32
step: 3,
33
//you will need to provide either an empty shippingMethod or paymentMethod for the event to send.
34
shippingMethod: '' // or paymentMethod: ''
35
});
36
37
//upon arrival at fourth checkout step ('Receipt' per the screenshot example above)
38
analytics.track('Viewed Checkout Step', {
39
step: 4
40
});
41
42
//upon completion of fourth checkout step ('Receipt')
43
analytics.track('Completed Checkout Step', {
44
step: 4,
45
//you will need to provide either an empty shippingMethod or paymentMethod for the event to send.
46
shippingMethod: '' // or paymentMethod: ''
47
});

You can have any number of steps in the checkout funnel as you'd like. The 4 steps above serve as an example. You'll still need to track the Order Completed event per the standard Ecommerce tracking API after you've tracked the checkout steps.

For client-side integrations, to use the ability to track Checkout Steps and Options, Segment uses Google Analytics' ProductAction class. You can read Google's developer docs for information on specific methods:

Enhanced Ecommerce allows you to go beyond measuring product performance to measure the internal and external marketing efforts that support those products. To take advantage of Enhanced Ecommerce's promotion reports, you can collect data about promotion impressions and promotion clicks with Analytics.js. For example:

1
analytics.track('Viewed Promotion', {
2
id: <id>,
3
name: <name>,
4
creative: <creative>, // optional
5
position: <position> // optional
6
});
7
analytics.track('Clicked Promotion', {
8
id: <id>,
9
name: <name>,
10
creative: <creative>, // optional
11
position: <position> // optional
12
});

If you want to send coupon data to your Order Completed event when using Enhanced Ecommerce, you can add the coupon property on the order level or the product level or both. In the below example, note that the Gtag destination accepts total or revenue, but not both. Segment recommends using revenue because several other destinations use revenue. For better flexibility and total control over tracking, you can decide how to calculate the way in which coupons and discounts apply. For example:

1
analytics.track({
2
userId: '019mr8mf4r',
3
event: 'Order Completed',
4
properties: {
5
orderId: '50314b8e9bcf000000000000',
6
total: 27.5,
7
shipping: 3,
8
tax: 2,
9
discount: 2.5,
10
coupon: 'hasbros',
11
currency: 'USD',
12
repeat: true,
13
products: [
14
{
15
id: '507f1f77bcf86cd799439011',
16
sku: '45790-32',
17
name: 'Monopoly: 3rd Edition',
18
price: 19,
19
quantity: 1,
20
category: 'Games',
21
coupon: '15%OFF'
22
},
23
{
24
id: '505bd76785ebb509fc183733',
25
sku: '46493-32',
26
name: 'Uno Card Game',
27
price: 3,
28
quantity: 2,
29
category: 'Games',
30
coupon: '20%OFF'
31
}
32
]
33
}
34
});

Measuring product impressions

measuring-product-impressions page anchor

Enhanced Ecommerce also allows you to collect impression information from users who have viewed or filtered through lists that contain products. This allows you to collect information about the products that users viewed in a list, which filters/sorts have they apply to a list of search results, and the positions that each product had within that list.

Product impressions map to the Product List Viewed and Product List Filtered Analytics.js events. You can find more information about the parameters and requirements in the Ecommerce tracking API.

Analytics.js allows you to collect this data and send it to a destination. For example:

1
analytics.track('Product List Viewed', {
2
category: 'cat 1',
3
list_id: '1234',
4
products: [
5
{
6
product_id: '507f1f77bcf86cd799439011',
7
sku: '45790-32',
8
name: 'Monopoly: 3rd Edition',
9
price: 19,
10
category: 'Games'
11
}
12
]
13
});

For refunds to work, you need to have Enhanced Ecommerce enabled.

For full refunds, fire this event whenever an order/transaction gets refunded:

1
analytics.track('Order Refunded', {
2
order_id: '50314b8e9bcf000000000000',
3
});

For partial refunds, you must include the productId and quantity for the items you want to refund:

1
analytics.track('Order Refunded', {
2
order_id: '50314b8e9bcf000000000000',
3
products: [
4
{
5
product_id: '123abc',
6
quantity: 200
7
}
8
]
9
});

Segment supports the following Google Analytics features:

Client-Side library methods

client-side-library-methods page anchor

Because Segment's client-side snippet wraps the gtag.js JavaScript SDK, all gtag.js library methods that don't map to Segment methods are available client side. Although invoking a native library method won't send data to Segment or other Segment-enabled destinations, the method will send data to Google.

To access gtag.js methods while using Segment, write these methods inside an analytics.ready() function, for example:

1
analytics.ready(function(){
2
window.gtag('config', 'GA_MEASUREMENT_ID');
3
})

To anonymize the IP addresses of all hits sent to Google, enable the Anonymize IP setting in the destination settings.

Segment uses Google's remarketing (the remarketing tag formerly known as Doubleclick) to tag visitors for remarketing campaigns and to identify demographic and interest data on visitors that displays in Demographic & Interest reports inside of Google Analytics.

Enable the Remarketing, Display Ads, and Demographic Settings in the Google Analytics destination to access these features.

Since remarketing loads through Segment, Google Analytics cannot validate that the code is present on the page. Click Skip validation and your data will start showing up within a few hours.

Although Segment does not support loading multiple trackers of the same type (for example, multiple web measurement IDs) through the destinations settings page (you will probably run into Google Analytics's rate limits(link takes you to an external page)), you can load a 2nd tracker on the page manually.

Here's how you'd initialize configure the second tracker:

1
analytics.ready(function(){
2
window.gtag('config', 'GA_MEASUREMENT_ID_2');
3
})
(information)

Info

The data translation/properties mapping must be set up in the .on() function before you send the event to Google Analytics. See the destination code(link takes you to an external page) for more information.

To do this server side, you can create a separate source in Segment, and within this source enter your Google Analytics credentials for the second tracker.

This source can be your server-side source. From there, you send data to multiple projects server-side. As you can see in this Node example you can initialize multiple instances of the library.

Segment supports Gtag tracking across multiple top level domains, but it requires a bit of work from you. See below for the two methods you can use to configure cross-domain tracking.

Tracking visitors with user-ID

tracking-visitors-with-user-id page anchor

If you're identifying your users with a user ID, cross-domain tracking becomes simple. All you have to do is make sure you identify your users on each domain and Google will merge those users together as one.

The caveat with this approach is that it works for identified users, anonymous visitor sessions do not persist across domains.

Tracking anonymous visitors

tracking-anonymous-visitors page anchor

When a visitor comes to your website, domain1.com, Gtag sets a first-party cookie that represents that user. That cookie looks like 182119591.1441315536, and ties to domain1.com (making it a first party cookie).

When your visitor clicks a link to go another domain, for example domain2.com, you'll need to tell the new site about the domain1.com cookie. To do this, rewrite your domain2.com links to include this domain1.com cookie. For example:

http://company2.com?_ga=1.182119591.1441315536.1362115890410

Gtag provides an auto-linking plugin to make this easier. To access the gtag methods while using Segment they must be inside an analytics.ready() function, which should appear after your basic Segment snippet, like this:

1
analytics.ready(function () {
2
window.gtag('config', 'GA_MEASUREMENT_ID', {
3
'linker': {
4
'domains': ['domain2.com']
5
}
6
});
7
});

For more advanced cross-domain implementations Segment recommends you follow the Google's guide to Measure activity across domains(link takes you to an external page).

To populate the Site Search report in Google Analytics, complete the following steps:

  1. When someone searches on your site, you must add the search term they used to the URL query, like this: domain.com?s=coconuts. The key ("s" in this case) can be any letter or string of letters.

  2. In your Segment source destinations catalog open the Gtag settings, scroll down to the Page Call settings and make sure to check the box for Include Query String.

  3. Inside Google Analytics, go to the Admin section, then click View Settings for the view you want to add Site Search to. Turn on Site search Tracking and enter the string from #1 into the Query parameter field. In this example it'd look like this:

    Google Analytics Site Search Settings with tracking enabled and query parameter 's'.

When you use Segment to load Gtag, the Segment script loads the gtag.js script. If you use Google Analytics as the verification option(link takes you to an external page) in Google Webmaster Tools, you'll need to switch to the Meta tags verification option(link takes you to an external page) instead. This will require you to find the <meta name=google-site-verification" ..> tag in Webmaster Tools and place it in your master HTML template.

Segment handles tracking the canonical URL to Google Analytics for you automatically. As long as there is a <meta rel="canonical"> tag on your page, Segment ensures Google Analytics gets the right canonical URL from it.

If you'd like to integrate with Google Analytics' Optimize plugin(link takes you to an external page), insert your Optimize Container ID in the destination settings and Segment will require the plugin when Google Analytics initializes.

You may want to deploy Google's anti-flickering snippet(link takes you to an external page) to prevent the page from flashing / flickering when the A/B test loads, as recommended by Google. You must add this code manually, since it needs to load synchronously.

(success)

Success!

Include the Optimize container ID in the anti-flickering snippet.


Metrics vs. Dimensions

metrics-vs-dimensions page anchor

Metrics and Dimensions both track custom data properties in Google Analytics. Metrics are for event properties with a numeric data type and Dimensions are for event properties with a string data type.

Google Analytics doesn't process data in real time in most of the Google Analytics reports. The easiest way to see if the data is streaming in is to check the Realtime reports in Google Analytics.

If you see events in your Realtime reports and those events never show up in other reports, that is due to a filter you have applied. You can see your active filters inside Google Analytics by clicking Admin then under your View on the right click Filters.

This article does a great job of explaining GA self referrals and how to fix them: https://threeventures.com/how-to-fix-self-referrals-in-google-analytics/(link takes you to an external page)

Google Analytics's default reporting time frame is a month ago to yesterday. You'll need to adjust it from a month ago to today's date to see today's already processed events.

If your site uses https://, please go to your Google Analytics property settings page and change your Site URL to use the https:// protocol.

Using Segment won't affect your bounce rates in Google Analytics. If you see your bounce rates drop after installing Segment make sure you don't have multiple copies of Segment's snippet on your page. Also be sure you're not calling page more than once when the page loads.

If you call track on page load make sure to set nonInteraction to 1. You can also set all events to be non-interactive by default in Advanced Options. Read more in the non-interaction events section.