AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
4.3K members online now
4.3K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Imlementation of cross-domain tracking in iframe

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello. Help Please!! I try to setup a cross-domain tracking for iframe, but I loose cookies. 

In more details:

There are a page http://www.hosteldubai16.com/rooms-rates.php in which iframe page https://hotels.cloudbeds.com/reservation/eLdcWS is implemented. I want to link site visits of www.hosteldubai16.com and the actions in iframe. So I use cross-domain tracking. Code implemented in www.hosteldubai16.com and on page https://hotels.cloudbeds.com/reservation/eLdcWS is the same: 

 

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-83538530-1', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['cloudbeds.com', 'hosteldubai16.com'] );
ga('send', 'pageview');
</script>

 

But when user come from Google (or another source) on page http://www.hosteldubai16.com/rooms-rates.php Google Analytics shows 2 different Users:

1 from Google, which is situated on http://www.hosteldubai16.com/rooms-rates.php

And 1 from (direct)/(none), which is situated on https://hotels.cloudbeds.com/reservation/eLdcWS

a81b4cef4c2d49be971d6d7761f8fbed.pngScreenshot_1.png

 

Why so? The user which is on the page https://hotels.cloudbeds.com/reservation/eLdcWS should be from Google, not from  (direct)/(none). The source of traffic is lost in iframe. Where is my mistake? How can I transfer source of traffic from hosteldubai16.com to hotels.cloudbeds.com? Maybe my analytics code is incorrect?

 

Thank you very much for advances!

1 Expert replyverified_user

Imlementation of cross-domain tracking in iframe

Top Contributor
# 2
Top Contributor

Hi :-)
Cross domain tracking involving iframes requires a different setup

You may want to refer the developer documentation

https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#iframes

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Imlementation of cross-domain tracking in iframe

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hi 

 

I am facing same problem in my analytics code. I have followed the steps given on https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#iframes but it's still not working.

 

Here are the steps that I have followed

1. Added both domains in referral exclusion list

2. Fetched client id with the help of the tracker in parent frame and passed it to iframe through postMessage

3. Used the client id received from parent frame and did ga create with the attributes

 

But when I send the pageview/event from iframe it still starts the new session.

 

For reference, you can check the link https://www.protrainingprograms.com/training_programs/rugby-league. Find buy now button on the page, when you click on it, it shows the iframe. On iframe opening I send a pageview and event.

 

Please let me know if I am missing something. Thanks in advance

 

Re: Imlementation of cross-domain tracking in iframe

Top Contributor
# 4
Top Contributor

Hi @akshay v :-)

 

I'm not locating the iframe tracking code anywhere

 

I have located the following for what appears to be normal cross domain tracking, however there are errors/inconsistencies in the code in relation to its general setup and even for cross domain tracking without iframes involved

 <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-23722888-1', 'protrainingprograms.com', {'name': 'sampleTracker', 'allowLinker': true, 'cookieDomain': 'protrainingprograms.com'});
ga('sampleTracker.require', 'linker');
ga('sampleTracker.linker:autoLink', ['protrainingprograms.com']);
ga('sampleTracker.send', 'pageview');</script>
Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Imlementation of cross-domain tracking in iframe

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Hi Bronwyn V

 

Thank you so much for looking into this. 

 

> I'm not locating the iframe tracking code anywhere

 

When you click on the buy button on this page - https://www.protrainingprograms.com/training_programs/rugby-league, iframe code gets executed.

 

Here is the code that we are using in the iframe.

 

```

ga('create', UA-xxxxxxx-1, 'gumroad.com', { clientId: <id-that-we-receive-from-parent-frame, name: "sellerTracker", allowLinker: true});
ga('sellerTracker.require', 'linker');

ga('sellerTracker.linker:autoLink', ['www.protrainingprograms.com']);

ga('sellerTracker.send', 'pageview');

```

 

In parent site, we have tried using following code as well, but it didn't work.

 

```

ga('create', 'UA-xxxxxx-1', 'parent-site.domain', {'name': 'sampleTracker'});
ga('sampleTracker.send', 'pageview');

```

 

Here is how we find the client id from the tracker and send it to the iframe:

 

```

if (typeof window.GoogleAnalyticsObject === "string" &&
      typeof window[window.GoogleAnalyticsObject] === "function" &&
        typeof window[window.GoogleAnalyticsObject].getAll === "function") {

  var trackers = window[window.GoogleAnalyticsObject].getAll(),
  tracker = typeof trackers === "object" ? trackers[0] : null;

  if (typeof tracker === "object" &&
        typeof tracker.get === "function") {
    return tracker.get('clientId');
  }
}

```

 

Can you please tell us what is wrong with the code that you have posted? We will change it.

 

So this is what we are doing

1. We ask the parent's site user to initiate the ga code in their HTML.

2. We ask them to load our JS on their site.

3. In our JS, we find the client ID and send it to the iframe that is already present in the parent site HTML through postMessage

4. In iframe js, after receiving the client id we initiate GA. 

Please let me know if we are missing anything.

Re: Imlementation of cross-domain tracking in iframe

Top Contributor
# 6
Top Contributor

To answer the first question. 
If you were cross domain tracking without iframes being involved.

 

1) Same GA property is used for both sites.
2) Both domains get added to the GA Property's Referral Exclusion List

3) a new View is created with a prepend hostname to request URI filter applied to the new View.


GA tracking code on domain1.com and domain2.com can be simplified down to a single snippet,

with the same snippet being placed on both domains.

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto', 'sampleTracker', {allowLinker: true});
ga('sampleTracker.require', 'linker');
ga('sampleTracker.linker:autoLink', ['domain1.com', 'domain2.com']);
ga('sampleTracker.send', 'pageview');
</script>
<!-- End Google Analytics -->

 


When you click on the buy button on this page - https://www.protrainingprograms.com/training_programs/rugby-league, iframe code gets executed.

 

There are a number of "buy now" buttons on that page, the first two do not trigger the iframe, they seem to be anchor links both linking to https://www.protrainingprograms.com/training_programs/rugby-league#special-offers further down in the page.
The "buy now" buttons further down the page in this Choose your Training Program section do appear to trigger the iframe.

 

Iframe tracking is not something i have dealt with in a number of years.

 

The best guide i can find for you in relation to analytics.js is the following one by GA Cert. Partner Co, Lunametrics

https://www.lunametrics.com/blog/2015/11/18/tracking-complex-interactions-in-iframes-google-analytic...

(it includes instructions both for analytics.js in the page and also if using GTM)

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Imlementation of cross-domain tracking in iframe

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

I have been trying to setup Google Analytics cross-domain tracking of events with iframe, but when I visit from Facebook, the events sent from iframe don’t get attributed with the source traffic instead they get attributed with Direct traffic.

 

Here is the sample code that I am trying - https://github.com/akshay-vishnoi/ga-sites

Just follow the README to set up the two rails application. I have followed the official documentation but it doesn’t work.
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#iframes

 

Please let me know if I am missing something. Thanks in advance for looking into the problem.