Using Google Analytics in Flash

By | April 19, 2010

Using Google Analytics in Flash is a very straightforward approach, I just found some of my Flash Project with one so might as well share how I made it.

What do you need?


  1. Your Flash Code
  2. Analytics Library for Flash which you can get here
  3. Definitely and Account with Google

Now once you have everything in place just add the Analytics on your library by going to File -> Import -> Import to Library.

Import To Library

Once imported you can see it on your library and use it.

GA in Library

Once its there you can now add a reference of the library in your codes as such

import com.google.analytics.API;
import com.google.analytics.AnalyticsTracker;
import com.google.analytics.GATracker;

If you want to check the version you are using add this piece ot your code

trace(API.version);

Now for the code

var tracker:AnalyticsTracker = new GATracker( this, "XX-9999999-1", "AS3", false );

//track on click and separate in different folders
var onButtonClick:Function = function( event:Event ):void
{
 tracker.trackPageview( "/Your Folder/" );
 var request:URLRequest = new URLRequest("http://www.test.com");
 navigateToURL(request, "_self");
}

TrackBtn.addEventListener( MouseEvent.CLICK, onButtonClick );

Now lets dissect whats happening:

on 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 thrid parameter is the tracking mode – you have a choice of either AS3 or Bridge.  So whats 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 on the code tracker.trackPageview( “/Your Folder/” )

This is just simply incrementing the pageview count for the virtual page “/Your Folder”

So thats its really simple and straight forward


Leave a Reply