Problem:
I was working on an international website for Le Pain Quotidien.
Depending on which country is chosen on the homepage, an other Google Analytics Tracker had to be used.
Solution:
I wrote a class which allows to create trackers at Runtime, and send tracking information to a specific tracker.
How it works:
The system consists of 2 classes:
- An AnalyticsManager class, which allows to add or remove trackers at runtime.
- An AnalyticsTracker class, which uses ExternalInterface to send the tracking-information to it’s associated tracker.
Code in AS3:
var analytics:AnalyticsManager = new AnalyticsManager ();
analytics.addTracker ("pageTracker1");
analytics.addTracker ("pageTracker2");
analytics.getTracker ("pageTracker1").track ( "tag_for_tracker_1" );
analytics.getTracker ("pageTracker2").track ( "tag_for_tracker_2" );
Code in HTML:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker1 = _gat._getTracker("UA-xxxxxxx-1");
var pageTracker2 = _gat._getTracker("UA-xxxxxxx-2");
pageTracker1 ._trackPageview("/home");
</script>
Files:
You can download the files here:
AnalyticsManager.zip
The class uses a HashMap to save the trackers.
The HashMap classes are included in the zip-file.
#1 by Maarten - March 23rd, 2009 at 18:46
thx Dries, that helped a lot