Using Google Analytics in Flash is a very straightforward approach, I just found some of my Flash Projects with one so might as well share how I made it.
What do you need?
- Your Flash Code
- Analytics Library for Flash which you can get here
- Definitely an Account with Google
Now once you have everything in place, just add the Analytics to your library by going to File -> Import -> Import to Library.
Import To Library
Once imported, you can see it in your library and use it.
GA in Library
Once it's there, you can now add a reference of the library in your code as shown below:
If you want to check the version you are using, add this piece to your code:
Now for the code:
var tracker:AnalyticsTracker = new GATracker( this, "XX-9999999-1", "AS3", false );
var onButtonClick:Function = function( event:Event ):void
tracker.trackPageview( "/Your Folder/" );
var request:URLRequest = new URLRequest("http://www.test.com");
TrackBtn.addEventListener( MouseEvent.CLICK, onButtonClick );
Now let's dissect what's happening:
In the code,
new GATracker( this, "XX-9999999-1?, "AS3?, false );
- The second parameter would be your Web Property ID – This is also known as the UA number of your tracking code and looks like UA-xxxxx-yy.
- The third parameter is the tracking mode – you have a choice of either AS3 or Bridge. So what's the difference? Bridge Mode is used if you control both the HTML page and the Flash content (if you own the webserver, you are hosting your page or at least have control on it). This mode is best if you have already implemented Google Analytics (ga.js) tracking on your website and you want to add tracking to embedded Flash content.
AS3 Mode is used if you control the Flash ActionScript 3 code, but you do not own or at least control the webserver. This is completely independent of the Google Analytics (ga.js) tracking and contains all the tracking functionality. This is good to use if you want to distribute your Flash applications to different sites.
- The fourth parameter is the debugging mode - I guess this is self explanatory.
Now in the code:
tracker.trackPageview( "/Your Folder/" )
This is just simply incrementing the pageview count for the virtual page “/Your Folder”.
So that's it! It's really simple and straight forward.