Iframe Cross-Domain Tracking In Google Tag Manager

What is cross-domain tracking?

Cross-domain tracking is a term used by Google to circumvent the web browser’s same-origin policy (as per the policy cookies can only be shared with parent domain and it’s sub-domains)
The Google Analytics calculates sessions and users by using a cookie, so if cookie is not persistent between 2 domains (from domain one to domain two) then Google Analytics will record 2 different sessions (the user on domain two will be different then the user on domain one)

If you want to know more about cross-domain tracking, you can read more about it here:

In this article, I will talk about iframe tracking. We will be using hitcallback to decorate/change iframe attribute and keep cookie persistent across iframe and parent domain.


1) Create custom javascript variable:

Name variable as “JS – hitCallback for X-Dom iframe

Code: Insert following code

function() {
return function() {
try {
var gobj = window[window.GoogleAnalyticsObject];
var iframe = document.querySelector('#myIframe'); //here iframe id is "myIframe, in case you don't have an iframe id then use iframe class or get tag and change source"
var tracker, linker;
if (gobj) {
tracker = gobj.getAll()[0];
linker = new window.gaplugins.Linker(tracker);
iframe.src = linker.decorate(iframe.src);  // here we are changing iframe source to load along with client id.
} catch(e) {}

Note: If you need step by step understanding of the code, you may check Simo article

2) In your pageview tag set allow the linker to true and set a hitCallback value to “JS – hitCallback for X-Dom iframe” in a field to set. Please check the screenshot below for your reference.


Note: Both Parent and iFrame URL must have same GTM code.

3) In a cross-domain tracking section, In an  “Auto Link Domains” field add both the domains i.e. current domain and iframe domain. Please check the screenshot below for your reference.



4) In your pageview tag trigger, add iframe domain as well, so it gets fired for iframe URL as well.


Here’s the process of what happens when you next browse the site:

  • The Page View Tag fires, and after it has completed, its hitCallback field is executed
  • Since this field had our custom function returned, the code within is run
  • This code takes the tracker that was just created and decorates the iframe(s) of your choice with cross-domain tracking parameters
  • The iframe will reload (because its src value changed) with the cross-domain tracking parameters

5) Test your implementation:

  1. Go to the parent domain URL, for an example //www.dhltaiwanconnects.com/shipping-oneoff-google.html?utm_source=tests3&utm_medium=testm3
  2. Start recording in a Google Tag Assistant
  3. Reload page
  4. Stop recording and click on the “show full report”
  5. Go to the Google Analytics Report
  6. Check the number of sessions, it should be one



  1. Go to the parent domain URL, for an example //www.dhltaiwanconnects.com/shipping-oneoff-google.html?utm_source=tests3&utm_medium=testm3
  2. Check Google Tag Assistant
  3. It must fire 2 pageviews i.e. of current domain and of iframe domain
  4. Check CID under the URL. It must be same for both the pageviews.




This was a rather simple guide for setting up iframe tracking with cross-domain parameters.

You may also like...

Leave a Reply