Using Google analytics tracking in Adobe Flash applications

Google Analytics a.k.a (GA) is a free service offered by Google that generates detailed statistics about the visitors to a website. So far, this service was available only for non-flash applications, but in present there is also a component that can be implemented in your Flash-driven content. You can find more info about this here. Let’s build now our first Adobe Flash application that will contain Google analytics that will track our actions.

Settings all files

Create a new ActionScript File and save it as “MainClass.as” (com.fladev.analytics.MainClass) – this will be the main class for our flash file; the package classes extends the Sprite class (since we don’t have a timeline animation). Finally create a new AS3 Flash file (590×300px) and open it, so that we have both files opened.

MainMovie

There are several ways to do this, but I will show you here the most common way to implement the Google analytics tracking. In order to do this to work you will need also an web property ID, also known as the UA number of your tracking code and looks like UA-xxxxx-yy.
In main movie create a simple movie clip, with instance name “btn”, that will trigger the tracking code when will be released. Should look something like this:

MainClass.as

Let’s add some listeners to our button:

[as3]package com.fladev.analytics
{
import flash.display.Sprite;
import flash.events.MouseEvent;

public class MainClass extends Sprite
{

public function MainClass() {
btn.buttonMode = true;
btn.mouseChildren = false;

btn.addEventListener(MouseEvent.ROLL_OVER, over);
btn.addEventListener(MouseEvent.ROLL_OUT, out);
btn.addEventListener(MouseEvent.CLICK, release);
}

private function release(e:MouseEvent):void
{

}

private function out(e:MouseEvent):void
{
e.target.gotoAndPlay(11);
}

private function over(e:MouseEvent):void
{
e.target.gotoAndPlay(2);
}

}
}
[/as3]

Get the Component Files

The Flash tracking component files are compressed into a single ZIP file that you can download from http://code.google.com/p/gaforflash/downloads/list. If you need anything else to know, just follow the instructions in the readme.txt file to install the Flash components.

Using The Analytics Component

After you download and import all tracking component files, we are ready to make our first example. The button that was just created, will be tracked when someone clicks it. When the button is clicked, the “release” method is called, which in turn invokes the trackPageview() method. This increments the pageview count for the virtual page “/myPage”.

First step is to import all classes:

[as3]import com.google.analytics.AnalyticsTracker;
import com.google.analytics.GATracker;
[/as3]

Then to declare a variable for your tracker:

[as3]private var tracker:AnalyticsTracker;
[/as3]

In constructor function to set the tracker with UA number:

[as3]
tracker = new GATracker( this, "UA-111-222", "AS3", true );
[/as3]

And last thing is to invoke the trackPageview() method:

[as3]
private function release(e:MouseEvent):void
{
tracker.trackPageview("/myPage");
}
[/as3]

If you release now the button, you will see some kind of a list with all tracking details:

To turn this off, just set the parameter to “false” in GATracker method:

[as3]
tracker = new GATracker( this, "UA-111-222", "AS3", <strong>false</strong>);
[/as3]

That’s it, if you have any questions do not hesitate to write me. Cheers!


Twitter Digg Delicious Stumbleupon Technorati Facebook

4 Responses to “Using Google analytics tracking in Adobe Flash applications”

  1. Thanks!

    You should write a continuation of this article for tracking events as well.

  2. i have bookmarked for future referrence !. Greetings from the Speedy DNS.

  3. Thank for this well writed article. I will visit this blog more! How can i stay updated? (RSS) or something??

  4. thank you

    i just pay for that and i am waiting for your file

    good work . continue