01.09Tutorial - Google Analytics (Event) Tracking for Flash / Flex
Hi Everyone,
as of sometime in November, Google has released a AS3 module that enables communication directly with Google Analytics, w/o the constraints of passing through Javascript. This combined with the imminent public deployment of Event Tracking, makes for a very exciting combination indeed!
This post is a follow-up to a previous post: Using Google Analytics to track activity inside of a Flash or Flex AS3 application, I suggest reading it as it will give you in-depth details on how google analytics works.
There are a few situations where you would like to use the AS3 module instead of the Javascript API to track activity in your flash application. For example:
- Your app is distributed using Gigya, ClearSpring, WidgetAvenue or other distribution platform.
- Your app is hosted on a different host than the html page it is contained in (which would cause cross-scripting nightmares)
- Your app is an AIR app, hence no HTML around it
- You are a developer and don't want to have to embed your app inside of HTML to know if event tracking works! (my favorite!)
There are two ways to embed this new functionality in your application:
- The .SWC module is available here. It weights a minimum of 60k however
- You can download the source straight from SVN, and compile only the classes you require. This approach provided a 40 kilobytes inflation on my .swf
Gee guys, 40k for event tracking?
The main drawback from my perspective (from someone who has been programming since the days of 33.6kbps modems to access the internet), is the size of the library. I integrated the functionality straight from source into this application which wound up gaining 40k (pounds lol!). It's crazy. We get cool Tweening libraries at under 10k, and all this does is send requests to a "web service" or some sort, and gee, 40k! Mind you, Flex coders will think this is small....
Easy to use
Here is a quick sample, using event tracking. It is perfectly designed in my opinion when it comes to the API (Full Reference) itself, reallly simple.
-
import com.google.analytics.AnalyticsTracker;
-
import com.google.analytics.core.EventTracker;
-
import com.google.analytics.core.TrackerMode;
-
import com.google.analytics.GATracker;
-
-
// {...}
-
-
var tracker:AnalyticsTracker;
-
var evtTracker:EventTracker;
-
-
tracker = new GATracker(this, "UA-213981-30", TrackerMode.AS3, false);
-
evtTracker = tracker.createEventTracker("Video Clip Finder");
-
-
// {...}
-
-
evtTracker.trackEvent("Artist Top Tracks", artistName, topTracksAmount);
I mean, this is simple. As for the result..
![]()
Event Tracking 101
In Google Analytics, event tracking happens in 4 levels of detail:
- Category
- Action
- Label
- Quantifier
When you create an Event Tracker like so:
-
evtTracker = tracker.createEventTracker("Video Clip Finder");
The phrase "Video Clip Finder" becomes the "Category" of all events tracked through that event tracker.
Then when you track a specific event like so:
-
evtTracker.trackEvent("Artist Top Tracks", artistName, topTracksAmount);
The phrase "Artist Top Tracks" becomes the "Action", the content of the variable "artistName" becomes the "Label" and the content of "topTracksAmount" becomes the "Quantifier".
Google Analytics then does averages on the quantifier, so you could, for example, track the amount of time a certain video plays, and Google Analytics would average it for you.
Therefore, if you are doing a marketing campaign (ugh! I never do marketing gigs.. marketing is evil!!!) that plays three videos, to the selection of the user; you could see in one quick glimpse the average play time of each video. THAT is a serious advantage of Event Tracking.
That and the fact that it won't screw up your pageviews anymore. Big value there too.
Hope you enjoyed today's post. More to come shortly.
Cheers!
Related posts (automatically generated):

That was an amazing post, and it earned you a new RSS subscriber. Keep up the good work!
January 23rd, 2009 at 5:42 pm
Great post. You can also use the events API to visualize your website’s response time how real users see it, not crawlers. You can achieve this by populating the fourth parameter (value) by the response time and then charting it using Analytics. For more details see my blog post:
http://www.vineetmanohar.com/blog/2009/03/23/using-google-analytics-event-api-to-track-your-website-performance/
March 24th, 2009 at 9:09 am
Found this post extremely useful and to the point.
Thanks much!
June 5th, 2009 at 2:03 am
Very nice post, I’ve heard of using Google Analytics within flash games, but never actually looked it up. Thanks for sharing, I’ll definitely look into using this.
October 25th, 2009 at 11:55 am