Click here to Skip to main content
15,883,901 members
Articles / All Topics

Using Google Analytics in Flash

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Apr 2010CPOL2 min read 9.2K  
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.

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?

  1. Your Flash Code
  2. Analytics Library for Flash which you can get here
  3. 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:

C#
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 to your code:

C#
trace(API.version);

Now for the code:

C#
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 let's dissect what's happening:

In the code,

C#
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:

C#
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.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
-- There are no messages in this forum --