5

I'm trying to measure visitors in an iframe and the site containing the iframe. What I would like is that visitors clicks in the iframe are seen being from the same visitor as the containing site, but somehow it is seen as two seperate visitors.

I followed examples from http://www.blastam.com/blog/index.php/2011/02/google-analytics-cross-domain-tracking/, trimmed down to an even simpler version based on the comments about setDomainName not being needed anymore but with setDomainName I get the same result: a click on a page and a click on the iframe is seen as 2 clicks by 2 seperate visitors.

This is the code in my iframe

if (_gaq && gaAccount.length > 0){
        _gaq.push(['_setAccount', gaAccount]);
        _gaq.push(['_setAllowLinker', true]);
        //_gaq.push(['_setDomainName', 'none']);
        _gaq.push(['_trackPageview', 'mytestcountername']);
    }

And this is the code in the containing page:

<script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-9605474-4']);
            _gaq.push(['_setAllowLinker', true]);
            //_gaq.push(['_setDomainName', '.domain.nl']);
            _gaq.push(['_trackPageview']);

            (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
</script>

1 Answer 1

2

You need two things setup.

1) Cross domain tracking - and according to the Google Analytics docs setDomainName is needed.

2) Correct linking to your iframe:

_gaq.push(function() {
   var pageTracker = _gat._getTrackerByName();
   var iframe = document.getElementById('myIFrame');
   iframe.src = pageTracker._getLinkerUrl('http://www.my-example-iframecontent.com/');
});

See Google Analytics docs for details and further examples: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#trackingIFrames

2
  • Ok. I read that page but it was very unclear to me, and what didn't help is that a colleague said the code I showed was working elsewhere. I think that can't be right, as I am thinking I probably really need the code you show in point 2. I will try that tomorrow.
    – Maarten
    Commented Nov 2, 2012 at 12:23
  • 2
    @Maarten to help debug this you should use the GA Debug extension for Chrome which will output the GA calls to the Console window. It helps debug many Analytics tracker code issues in a fraction of the time. chrome.google.com/webstore/detail/google-analytics-debugger/…
    – joesk
    Commented Jan 31, 2013 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.