cwoebker
cwoebker

Reputation: 3288

Google Analytics - Custom iFrame Tracking

I made a small little Tic Tac Toe game that plays perfectly at http://tic.cwoebker.com. The actual tic tac toe game is loaded from (/tic) and embedded in an iframe.

Therefore others could easily embed the game on their own site if they wanted.

Tic Tac Toe

I am doing some event tracking inside of the iframe. Right now I have it setup so it fires a page view on both the main page and the actual game code inside the iframe.

I was wondering whether I could somehow only fire the page view for the iframe if its not embedded on http://tic.cwoebker.com but on another site.

So that everything thats tracked under root (/) is traffic on my site and everything tracked in the i frame (/tic) traffic generated by embedding on another site.

Right now my analytics code in the iframe looks like this:

 <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxxxx-x']);
    _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>

Thanks a lot in advance.

Upvotes: 1

Views: 2207

Answers (2)

Ravi Gidwani
Ravi Gidwani

Reputation: 308

You can use Google Analytics campaigns to track the traffic. You will need to distribute the embed code that already has the campaign parameters as part of the embed iframes src.

Hope that helps.

Interested to hear from others.

Upvotes: 0

TomFuertes
TomFuertes

Reputation: 7270

As you're in an iFrame on a separate domain, you can't access the parent window to get the location. Check out this demo and/or google it a bit: http://jsfiddle.net/TomFuertes/RRB52/2/

_gaq.push(['tictactoe._setAccount', 'UA-xxxxxxxxx-x']);
// Line below causes security issues, thus won't work
_gaq.push(['_setCustomVar', 1, 'Domain', window.parent.location.host, 3]);
_gaq.push(['tictactoe._trackPageview']);

You can pass the domain using a querystring on your iFrame page, you'd need to modify the include code to look like this: https://stackoverflow.com/a/5697801/94668

<script type="text/javascript">
document.write('<iframe src="//tic.cwoebker.com/?url=' + window.location + '"></iframe>');
</script>

Then you'd filter out your Google Analytics appropriately.

Upvotes: 1

Related Questions