Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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>
share|improve this question
add comment

1 Answer

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

share|improve this answer
    
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 Nov 2 '12 at 12:23
1  
@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 Jan 31 '13 at 11:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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