Home Contact RSS

Integrate Google Analytics with Flash | Tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

The results are in and the requested topic is “Integrate Google Analytics into Flash.” The poll has been reset and is ready to recieve your post requests, so keep voting! It’s located in the side bar!

Overview:

Tracking your visitors and attempting to better understand them is a large part of even having content on the web. Since the days of visitor counters displayed proudly on every site, along with dozens of animated gifs to the days when site were designed solely with efficiency and conversion in mind. There are many services that will do this for a fee and other that will do it for free. A popular free web analytics tool is provided by Google. Google Analytics is started by including JavaScript on each page the user wishes to track. This JavaScript loads larger files from the Google webserver and then sets variables with the user’s account number. This JavaScript is used to track and log page views and visitors interaction with the site. This post discusses what to do if your site includes a lot of interactive flash elements you wish to track as well. With the little Google has published related to this(New Code, Old Code), I’ve tried to fill in.
Note: This explains how to set up to track flash events as page views, which does have a drawback- it inflates your pageviews in the analytics and in turn may skew your data. Il’l beposting again soon about how to use then new event tracking, which would track flash events not as pageviews, but as events and thus not inflate the page view count.

Steps:

  1. If you haven’t already, install Google Analytics on your site. (Note that your analytics tracking code must be placed on the page above the flash call(s) to _trackPageview or urchinTracker)
  2. Determine which events in flash you want to track.
  3. Place in the external.interface code in your actionscript at the specific event(s).
  4. Watch the events get logged in you Google Analytics Reports!

Example:

Here’s a simple example, say you want to track how many times an object is clicked or dragged by a user or how many times it bounces (something that could be tracked but doesn’t necessarily have any required user interaction). In this example flash file I have a ball which bounces off the walls and users can click to drag and even throw it, press the spacebar to create more balls and toggle the gravity on and off with the arrow keys (up is weightlessness, and down is gravity). Each of these events has code to communicate with Google Analytics JavaScript and track the events. I made my own function to call the google analytics code. Do I hear “but sometimes I want to use the new version of Google analytics, and sometimes I want to use the old one…” Have no fear, this function works for either one, or even both. If you’re not using the newer code the calls to the functions in the new code (pageTracker._trackPageview()) will be ignored and vice versa, if you are using the new code, then the calls to the functions in the old code (urchinTracker()) will be ignored, since the functions are not defined. You can track virtually anything with this method. I’ve exaggerated greatly in this example- just to show the variety of different ways this can be used. You might want to make certain the things you track will be useful and relevant for you.

Here is the swf file, I’ve added a text box that will print all actions that are logged to Google Analytics
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

track Google Analytics actionscript function

  1. function trackGA(action:String) {
  2. //Old Google Analytics Code
  3. ExternalInterface.call("urchinTracker('/urchin/IntegrateGoogleAnalytics/"+action+"')");
  4. //New Google Analytics Code
  5. ExternalInterface.call("pageTracker._trackPageview('/pageTracker/IntegrateGoogleAnalytics/"+action+"')");
  6. trace("Google Analytics Tracking: " + action);
  7. }

Calls to Google Analytics actionscript function

  1. trackGA("swfLoaded");
  2. trackGA("ball/"+_root.id+"/created/");
  3. trackGA("ball/"+this.ballNum+"/released/");
  4. trackGA("ball/"+this.ballNum+"/pressed/");
  5. trackGA("ball/"+this.ballNum+"/bounced/top");
  6. trackGA("ball/"+this.ballNum+"/bounced/left");
  7. trackGA("ball/"+this.ballNum+"/bounced/right");
  8. trackGA("ball/"+this.ballNum+"/bounced/bottom");
  9. trackGA("gravity/on");
  10. trackGA("gravity/off");

Download:

IntegrateGoogleAnalytics.zip

Update:

Here is a screenshot of my Google Analytics Top Content after I search for “pageTracker/IntegrateGoogleAnalytics” (because I’m using the new code version, if I were using the old version I’d search for “urchin/IntegrateGoogleAnalytics”). This just shows that every event was logged to Google Analytics. This screenshot was taken mere hours after this post published.
Google Analytics Screenshot pageTracker/IntegrateGoogleAnalytics/

Stay tuned for upcoming post about the new Event Tracking rather than posting each event you want to track in flash as a pageview it can be a specific event!

Tags: , , , , , , , ,

Related posts

flashden banner

Gravatar

somnamblst said,

January 31, 2008 @ 10:05 am

I realize you have not done the event tracking blog post yet, but here is what I came up with last night to track rollovers in addition to an on (release)

In my ga.js I added

// Here is the new event object
var rolloverEventTracker = pageTracker._createEventTracker(’rollover’);

My AS:

box_mc.onRollOver = function():Void
{
ExternalInterface (”javascript:rolloverTracker._trackEvent(’rollover’, ‘MyRollover’);”)

box_mc.gotoAndPlay(2);
};

box_mc.onRollOut = function():Void
{
box_mc.gotoAndStop(1);
};
box_mc.onRelease = function():Void
{
getURL(”javascript:pageTracker._trackPageview(’/flash/click/’);”);
getURL(”http://www.yahoo.com”,”_blank”);
};

Normally the ga.js goes at the bottom of the page, immediately before the </body) tag. Do I need to put it above my Flash object code on the HTML page?

Gravatar

Evan Mullins said,

January 31, 2008 @ 11:17 am

Yes, I this is mostly correct, notice that the ExternalInterface.call() calls don’t need the ‘javascript:’ that is for getURL()
Although, since I’m not a beta tester of the new Google Analytics Tracking Code, I can’t access the event tracking in my reports yet…

And yes again, Here’s the answer straight from Google: “Google Analytics lets you track any browser based event, including Flash and JavaScript events by using the _trackPageview function, you can assign a page filename to any Flash action, and enter that filename into the appropriate goal or funnel step. Important: Please note that your analytics tracking code and calls to _gat._getTracker and _initData must be placed on the page above the call to _trackPageview.”

So it seems that the new GATC should optimally be included at the top of the page rather than the bottom. I just include it in the header rather than the footer.

I assume you need the event object placed before the flash, so the ga.js (exactly like you have it):
var rolloverEventTracker = pageTracker._createEventTracker(’rollover’);

and the AS:
box_mc.onRollOver = function():Void {
ExternalInterface.call(”rolloverEventTracker._trackEvent(’rollover’, ‘MyRollover’)”);
box_mc.gotoAndPlay(2);
};

Good luck! I’m anxious for this feature to be fully rolled out to all users!
I will confirm as soon as I’m able to test it out!

Gravatar

James said,

February 9, 2008 @ 5:58 am

Ooh, that’s pretty nifty. We’ve been doing things like this for other commercial tracking systems like Hitbox, but I didn’t realise you could also do it with Google Analytics…

Gravatar

Kathy said,

March 26, 2008 @ 9:52 pm

Thank you very much for your site and what you’ve put into this page… but I’m out of my element and am hoping someone can post: 1) a simple HTML page with embedded SWF w/ Play button - such that I can see exactly where and how the Google Analytics code is used in the HTML in relation to the SWF and tag. 2) Its corresponding FLA and any files for download (similar to what you’ve done here for your example).

Should it make any difference, I have a web page with a Flash file (when you hit “Play” button, it loads an FLV) I want to track IF and how long the Movie is viewed. I already have a listener on the Play button in Flash to “go to frame #” when movie ends. If Listeners apply to using GA and Flash — will these Listeners conflict?

I have another site, which is one Flash File - but loads 4 different FLV movies via buttons I created, each Play button has a listener. Stepsdancecenteronline.com/videos.html

Will I be able to use GA to track views & time viewed for this 4 movie Flash file? (that is, can this be done… not will I be able to set it up properly)

Again, I learn best by example, and what is frustrating to me, is that many great tutorials leave just one or two things to “assumption” of the skill level of the audience. If I can see the code in context, my learning curve goes up.

Thank you again, - even if you can’t accommodate this neediness on my part
Kathy

Gravatar

Andy said,

July 11, 2008 @ 11:51 am

I would have thought this example would have a higher bounce rate…

Gravatar

Andy said,

July 11, 2008 @ 11:52 am

because the ball…bounces.

Gravatar

Dan said,

July 23, 2008 @ 11:52 am

Excellent tutorial, and wonderful example provided. I’ve been looking for this info for a long time.

Thanks a lot for putting in the time for this tutorial. I never considered tracking if the SWF loaded correctly, but it is a very good idea.

I am looking forward to the Event Tracking blog entry.

RSS feed for comments on this post · TrackBack URI

Leave a Comment