gtag.js for AdWords conversion tracking and remarketing

gtag.js is a new web tagging library that replaces the AdWords website conversion tracking tag and the remarketing tag. Conversion tracking and remarketing functionalities are now combined into a single tag. In addition, gtag.js can be used to send data to some other Google products, including Google Analytics. This guide describes how to implement gtag.js for AdWords. Refer to this guide for details on how to implement gtag.js for Google Analytics.

Note that gtag.js is not intended to replace nor be used within Google Tag Manager. If Google Tag Manager is already implemented on your website, we recommend that you continue using the built-in tag templates for AdWords remarketing and conversion tracking, as well as the conversion linker tag, which will provide the same tracking accuracy.

At a high level, the model for working with gtag.js is as follows:

  • Install the global site tag on every page of your site and configure it to work with one or more AdWords accounts.
  • Use the gtag.js API to track events such as conversions or other meaningful user interactions with your website.

The gtag.js global site tag

Overview

The gtag.js global site tag is used to load the tracking library and configure the AdWords account(s) you intend to send data to.

Unlike the previous version of the AdWords conversion tracking tag and remarketing tag, gtag.js is an asynchronous tagging library, which means that the browser will download the tag without blocking the rendering of your page’s content.

The gtag.js global site tag should be installed on every page of your website, as high up in the <head> section of your page’s HTML code as possible for best tracking accuracy. If you are using Google Tag Manager or another tag management system, refer to this section for instructions on setting up conversion tracking and remarketing.

Below is an example of the gtag.js global site tag:

<!-- Global Site Tag (gtag.js) - Google AdWords: GOOGLE_CONVERSION_ID -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'AW-GOOGLE_CONVERSION_ID');
</script>

The GOOGLE_CONVERSION_ID placeholder value is a unique numerical ID that is tied to each AdWords account. You can find your global site tag with its associated conversion ID in the AdWords interface when you create a new conversion action or view an existing one or when you set up remarketing for your site.

Configuring the global site tag for multiple accounts

You only need one global site tag snippet per page. If you plan to send data to multiple AdWords accounts, simply add a call to the ‘config’ command for every account you’ll be using, specifying each account’s conversion ID, as shown in the example below:

<!-- Global Site Tag (gtag.js) - Google AdWords: GOOGLE_CONVERSION_ID_1 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID_1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'AW-GOOGLE_CONVERSION_ID_1');
  gtag('config', 'AW-GOOGLE_CONVERSION_ID_2');
</script>

You’ll notice that the global site tag has an ID embedded in its first line:

<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID"></script>

This ID is set when you obtain a global site tag (e.g. from an AdWords or Google Analytics account). The ID in this line (i.e. the gtag.js ID) does not determine which account(s) will receive data from the tag; rather, it is used to uniquely identify your global site tag. Which account(s) receive data from the tag is determined by calling the config command (and by using the send_to parameter on an event). For instance, if you use Google Analytics, you may already have the gtag.js global site tag installed on your site. In that case, the gtag.js ID may be that of the Google Analytics property where you first obtained the snippet. To update your global site tag to additionally send data to AdWords, insert an additional 'config' command for your AdWords account, as shown below:

<script async src="https://www.googletagmanager.com/gtag/js?id=GA-PROPERTY_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'GA-PROPERTY_ID');
  gtag('config', 'AW-GOOGLE_CONVERSION_ID');
</script>

Note that you don’t need to change the gtag.js ID.

The config command configures the tag for sending data to particular products and accounts. To configure the tag for sending remarketing and conversion data, you will add a config command with a conversion ID for each AdWords account to which you want to send data. For AdWords, the config command also fires a remarketing hit on page load. To prevent the global site tag from automatically sending a remarketing hit to your AdWords accounts when the page is viewed, update the relevant config command(s) as follows:

gtag('config', 'AW-GOOGLE_CONVERSION_ID', {'send_page_view': false});

The global site tag is sufficient to enable standard remarketing capabilities for your AdWords account(s). Every URL where the global site tag is installed is captured by AdWords, which gives you the ability to create audience lists defined with URL based rules.

To collect additional user interactions happening on your website, including conversions, you will need to call the event command when these specific actions occur.

Conversion linker functionality

To ensure that AdWords can measure all of your conversions, regardless of the browser that your site visitor is using, the global site tag creates a first-party cookie to store ad click information captured on your site's landing pages. This is why it’s critical that the global site tag is implemented on all pages of your site and that your AdWords accounts have auto-tagging enabled.

When auto-tagging is enabled for your AdWords account, AdWords automatically generates a click ID when a user clicks on one of your ads, and appends it to your landing page URL (e.g. via the gclid query string parameter). When configured for AdWords, gtag.js extracts ad click information from the URL (e.g. the value of the gclid parameter), and stores it in first-party cookies named _gcl_aw, scoped to your domain with a lifetime of 90 days.

For example, if your landing page is www.example.com/my/landing/page.html, the _gcl_aw will be scope to .example.com. This means it accessible to all subdomains of example.com.

When gtag.js is used to track a conversion, it will check for the _gcl_aw cookie(s) that may be available on the conversion page and, if found, send the most recent gclid value(s) to AdWords.

If you don’t want the global site tag to set first-party cookies on your site’s domain, customize the config command as shown below:

gtag('config', 'AW-123456789', {conversion_linker: false});

Note that this will result in less accurate conversion measurement.

The event snippet

Once you’ve installed the global site tag, you can send additional data to AdWords for remarketing and conversion tracking using the ‘event’ command.

gtag(event, event_name, optional_event_data);

The ‘event’ command takes two arguments:

  • The event name is a string that describes the event you want to capture. You can use any name you want, but we recommend using some of our standard event names.
  • The event data is an optional JavaScript object containing a list of key/value pairs that provides data about the event (conversion value, product IDs, etc.).

Here’s an example of how to use the event snippet to track a user registration:

gtag('event', 'sign_up', {
  'account_type': 'basic' // Optional data describing the type of account created by the user.
});

The example above fires a remarketing hit to the AdWords account(s) configured in the global site tag, along with the event name and its associated data.

If you have multiple AdWords accounts configured in the global site tag and you would like to send event data only to one of them, you can use the optional send_to parameter in the event data object to specify which account(s) should receive the data.

gtag('event', 'sign_up', {
  // Only send this event to AW-GOOGLE_CONVERSION_ID2
  'send_to': 'AW-GOOGLE_CONVERSION_ID2', 
  'account_type': 'basic',
});

To send data to multiple accounts, pass the list of account IDs using the array notation:

gtag('event', 'sign_up', {
  // Send this event to AW-GOOGLE_CONVERSION_ID2 and AW-GOOGLE_CONVERSION_ID3
  'send_to': ['AW-GOOGLE_CONVERSION_ID2', 'AW-GOOGLE_CONVERSION_ID3'], 
  'account_type': 'basic',
});

With the sample event tag above, you can use the event name to create an audience list targeting users who have registered on your website.

Every key passed in the event data object also becomes available in AdWords to build user list membership rules. For instance, you can target visitors who have created a ‘basic’ user account with a rule: ‘account_type’ equals ‘basic’.

You are free to pass any list of key/value pairs as event data, which lets you customize the data you collect for each event based on your tracking needs. As a best practice, we recommend that you send recommended event data parameters for standard events. The list of standard events and their recommended data parameters can be found here.

Since the ‘event’ command is a JavaScript function call, it can easily be implemented as part of an event callback in order to send data to AdWords in response to user-initiated events such as button clicks or form completion.

Here’s an example of how to fire the event snippet when the user clicks on an outbound link.

<script>
  function track_click_event(url) {
    var callback = function () {
      if (typeof(url) != 'undefined') {
        window.location = url;
      }
    };
    gtag('event', 'button_click', {
         'destination': url,
         'event_callback': callback
    });
    return false;
 }
</script>

<a onclick="return track_click_event('http://example.com/your-link');" 
   href="http://example.com/your-link">Click here!</a>

Note that in this example, the event data object has an optional event_callback field which takes as value a callback function that is executed after gtag.js has succesfully sent the data to AdWords. This is useful to trigger additional page behavior, such as redirecting the user to a destination URL, for instance.

Tracking conversions

Overview

Any event can be associated with an AdWords conversion action. Conversion actions are identified by a unique string which looks like: AW-123456789/AbC-D_efG-h12_34-567.

The conversion string is uniquely generated by AdWords once you’ve defined the conversion in the AdWords interface and is included in the pre-generated event snippet handed out:

<!-- Event snippet for CONVERSION_NAME conversion page -->
<script>
  gtag('event', 'conversion', {'send_to': 'AW-123456789/AbC-D_efG-h12_34-567'});
</script>

You can implement the event snippet as-is, or you can modify it to make it more meaningful. For instance, if you wanted to track the user registration event given previously as a conversion, simply add a ‘send_to’ field in the event data object, as shown below:

gtag('event', 'sign_up', {
  'account_type': 'basic', 
  'send_to': 'AW-123456789/AbC-D_efG-h12_34-567'
});

When tracking a conversion, the event snippet will attempt to read the current user’s ad click data that was stored by the global site tag in a first-party cookie on your domain. If found, the data will be passed to AdWords in the tag’s payload.

Passing the same event as conversion to multiple AdWords accounts

If you need to count the same event as conversion for multiple AdWords accounts, you can do so by passing multiple conversion ID strings via the send_to parameter of the conversion event snippet.

gtag('event', 'sign_up', {
  'account_type': 'basic', 
  'send_to': [
    'AW-123456789/AbC-D_efG-h12_34-567',
    'AW-987654321/Ijk-L_mnO-p98_76-543'
  ]
});

Alternatively, you should consider using cross-account conversion tracking which allows you to have a manager account (MCC) own the conversion actions and share them with one or more of its sub-accounts. In doing so, you only need to specify a single conversion identifier in the event snippet. Learn more about cross- account conversion tracking.

Sending dynamic conversion data

When tracking a conversion, you have the option to dynamically specify the conversion value, currency, and transaction ID. This is achieved by passing additional parameters as part of the conversion event data object, as shown below:

gtag('event', 'purchase', {
  'send_to': 'AW-123456789/AbC-D_efG-h12_34-567',
  'value': 345.89, 
  'currency': 'USD', 
  'transaction_id': '12345' 
});
  • The value parameter is used to report the value of each conversion to AdWords. If the conversion is an ecommerce purchase, it should be set to the total value of the transaction, the gross margin, or whichever business metric makes sense for your tracking goals.
  • The currency parameter should be set when a conversion value is provided, using ISO 4217 currency codes.
  • The transaction_id field lets you provide an optional identifier for the transaction. This ID is used by AdWords to de-dupe multiple conversion events that may be fired for with the same transaction_id and avoid counting the conversion multiple times. This sometimes happens when users can access a cached version of the conversion page, which may contain the event snippet embedded in it.

For the best conversion tracking accuracy, we recommend implementing the global site tag on every page of your site and using the event snippet on your conversion pages. This allows the global site tag to capture the gclid when a user lands on your site following an ad click, which the event snippet can then read and send back to AdWords.

Custom conversion tracking scenarios

If you have custom tracking needs or specific technical constraints, you can adapt gtag.js to track conversions in a way that suits your needs, but this may require advanced technical resources on your end to correctly implement the tag.

Conversion tracking implementation via Google Tag Manager

If you use Google Tag Manager, you do not need to tag your site with gtag.js in addition to your existing Google Tag Manager container code, nor should you deploy gtag.js via a custom HTML tag template. Simply make sure you have deployed the built-in AdWords conversion tracking tag template on the conversion page, as well as the conversion linker tag on every page of your website. The conversion linker serves the same function as that of the gtag.js global site tag for conversion tracking purpose, which is to capture and store the gclid in a first party cookie.

Note that if you are also using Google Tag Manager to deploy Google Analytics tags, you do not need to deploy the conversion linker tag. Instead, you can enable your Google Analytics tag to create a first party cookie to store gclids by linking the Google Analytics property associated with your deployed tag with the AdWords account for which you want to track conversions.

Conversion tracking implementation via tag management system

Most tag management systems offer built-in tag templates for AdWords conversion tracking or remarketing. When possible, you should use these tag templates rather than deploy gtag.js via custom JavaScript code injection because tag templates have generally been tested by your tag management system provider to ensure proper functionalities.

Note that some tag managament systems offer both JavaScript or image only tag templates for AdWords conversion tracking and remarketing. If this is the case, you should use the JavaScript version of the AdWords tag template.

If you use a tag management tool other than Google Tag Manager, we recommend one of the two setup options below.

If you have deployed Google Analytics tags via your tag management tool, you can enable gclid collection by linking your Google Analytics property to AdWords. In this case, you should just deploy the built-in AdWords conversion tracking tag template on the conversion page only, if available.

If your tag management tool doesn’t have a template for AdWords conversion tracking, you can deploy a page specific version of gtag.js on the conversion page only, shown below:

<script async src="https://www.googletagmanager.com/gtag/js?id=AW-1234567891072716560"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
</script>
<script>
  gtag('event', 'conversion', {
    'send_to': 'AW-123456789/AbC-D_efG-h12_34-567',
    'value': 345.89, 
    'currency': 'USD', 
    'transaction_id': '12345', 
});
</script>

Implement the gtag.js global site tag through your tag management system

If you have not deployed Google Analytics tags, you should deploy the gtag.js global site tag on every page of your site as well as the event snippet on the conversion page through your tag management system.

Be aware that this may require complex technical work to avoid certain pitfalls that may cause the tag to malfunction. In particular, you’ll need to ensure that the event tag is injected by your tag management tool after the global site tag, since it requires gtag.js to have been loaded in memory by the browser in order to execute properly.

You should also make sure that your tag management system injects the tags directly in your page’s DOM and not in an embedded iframe.

Measuring conversions for multiple AdWords accounts

It's important to note that gtag.js attributes a conversion to the most recent click that preceded the conversion, even when a user clicked on multiple campaigns that belong to different AdWords accounts. This is because a new ad click overwrites any gclid cookie that might have been previously set. When the user completes a conversion, gtag.js will read the gclid value set by the most recent ad click and will not have access to previous gclid values.

If you use multiple AdWords accounts to drive traffic to a website tagged with gtag.js and would like to measure conversions independently for each account, you will need to customize gtag.js as well as your ads destination urls in order to use different cookies to store the gclid for each of your AdWords accounts.

As an example, let's say that your want to track conversions for two AdWords accounts:

  • AdWords account A, with conversion ID GOOGLE_CONVERSION_ID_1
  • AdWords account B, with conversion ID GOOGLE_CONVERSION_ID_2

To do so, you need to be able to distinguish users coming from A's campaigns from users coming from B's campaigns. This can be done by appending a parameter or hash fragment to your ads' final URLs.

  • For A's campaigns: www.mywebsite.com/my/landing/page.html#aw=A
  • For B's campaigns: www.mywebsite.com/my/landing/page.html#aw=B

Next, configure the gtag.js global site tag to use different cookie names, based on conditional logic that uses the landing page URL.

<script>
   var aw_prefix = location.href.contains('aw=A') ? 'A' : 'B';
</script>
<!-- Global Site Tag (gtag.js) - Google AdWords: GOOGLE_CONVERSION_ID_1 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID_1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'AW-GOOGLE_CONVERSION_ID_1', {conversion_cookie_prefix: aw_prefix});
  gtag('config', 'AW-GOOGLE_CONVERSION_ID_2', {conversion_cookie_prefix: aw_prefix});
</script>

The code above will store gclid values coming from A's campaigns in a cookie named A_aw and those coming from B's campaigns in B_aw (the _aw suffix is automatically added by gtag.js to the custom cookie name you provide). Note that it doesn't matter that we use the same cookie name for A's and B's config commands in the global site tag that is on your landing pages since on those pages, the global site tag only serves to write the gclid cookie value.

The conversion pages is where the gclid cookie is read and for those pages only, we need to tell gtag.js to use different cookies when firing a conversion action owned by A versus a conversion action owned by B. To do so, configure the global site tag as shown below:

<!-- Global Site Tag (gtag.js) - Google AdWords: GOOGLE_CONVERSION_ID_1 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID_1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'AW-GOOGLE_CONVERSION_ID_1', {conversion_cookie_prefix: 'A'});
  gtag('config', 'AW-GOOGLE_CONVERSION_ID_2', {conversion_cookie_prefix: 'B'});
</script>

If the same user action should be counted as a conversion for both A and B, call the event snippet, passing both conversion action identifiers, as shown below:

gtag('event', 'conversion', {
  'send_to': [
    'AW-GOOGLE_CONVERSION_ID_1/CONVERSION_LABEL_1',
    'AW-GOOGLE_CONVERSION_ID_2/CONVERSION_LABEL_2',
  ]
});

Validating your conversion tracking implementation

First, you should validate that the global site tag is correctly setup to capture the gclid from any landing page on your site and store it in a cookie.

To do so, pull up any of your site’s URLs and append a gclid parameter with any test value. For instance www.mywebsite.com/home.HTML?gclid=12345.

Then, open Chrome Developer Tools and click on the “Application” tab, then cookies, and look for a cookie named _gcl_aw.

If you can find the _gcl_aw cookie, it means that the global site tag is correctly set up for tracking AdWords conversions.

Next, you should complete a test conversion manually and trigger the conversion event snippet. When you reach the conversion page, you can use Tag Assistant to verify that the conversion event is firing correctly.

When the conversion event snippet is executed, you should see 3 http requests fired by the browser, going to:

  • https://www.googleadservices.com/pagead/conversion
  • https://googleads.g.doubleclick.net/pagead/viewthroughconversion
  • https://www.google.com/ads/conversion

To validate that the conversion event snippet is able to read the first party cookie that contains the gclid, inspect the URL of the request made to googleadservices.com. Assuming you have confirmed the presence of the _gcl_aw cookie as described above, the request to googleadservices.com should contain a query string parameter named gclaw whose value should match that of the gclid stored in the _gcl_aw cookie.

Additional troubleshooting tips can be found in this article.

Dynamic Remarketing

Dynamic remarketing can be implemented via gtag.js. The global site tag provides standard remarketing tracking capabilities, by collecting every URL on which the tag fires, which lets you create URL-based audience lists.

In addition, the event snippet allows you to capture meaningful events that occur on your site along with related data.

Dynamic remarketing allows you to automatically tailor your ads based on the products or services your users have interacted with during previous visits.

To implement dynamic remarketing, you must first visit the Audience manager section of AdWords and configure your AdWords data source and indicate the relevant business type(s) applicable to your website.

Depending on the business type(s) you select, you will need to pass specific data to the gtag.js tag via the event snippet.

Refer to the parameter reference section for a detailed list of event data parameters you should use for each business type.

As an example, let’s assume that you are setting up dynamic remarketing for the retail business type. There are 3 event data parameters that should be implemented when setting up the retail vertical: ecomm_prodid (required), ecomm_pagetype (optional), ecomm_totalvalue (optional).

When meaningful interactions occur on your site, you should call the event command to send the associated retail event data, as shown in the example below.

<script>
  gtag('event', 'add_to_cart', {
    'ecomm_prodid': ['12345', '45678'],
    'ecomm_totalvalue': 345.89, 
    'ecomm_pagetype': 'cart'
});
</script>

All the values shown here are for illustration purpose and should be dynamically passed to the event snippet at runtime with values corresponding to the actual content of the user’s shopping cart.

The event name you use (e.g. ‘add_to_cart’ in the example above) can be a custom event name of your choosing or one of our recommended standard events.

It doesn’t need to match any specific value for the purpose of implementing dynamic remarketing, as long as the event data parameters (ecomm_pagetype, ecomm_prodid, ecomm_totalvalue in this example) are correctly set.