Tracking Flash stats with Google Analytics

One of the first things deployed after developing a new website is some sort of stats tracking. The insights that can be gained from analyzing the performance and user metrics can be invaluable – so it would be a shame to drop a load of cash on a swankin’ Flash site to have the only stat you get back is how many times the index page gets stomped by the users.

If you use Google Analytics then it’s easy to the point of it being a crime if you don’t use it to some extent.

Once you have your site setup within the Analytics interface, and dropped your Google prescribed code in your page, you will need to create a function within your Flash site to fire off events to the JavaScript function to track as much or as little as you need. The function you want to concern yourself with is this one:

urchinTracker();

That’s what will create your entries with the Analytics reporting interface. Now create a simple Flash Function to do the work.

function trackSectionData(sectionArg,subSection):Void{
    var dataToTrack:String = this.titlesArray[sectionArg];    
    if (subSection == 0){
        getURL("javascript:urchinTracker(" + dataToTrack + ");");
    } else {
        if (sectionArg == 1){
            dataToTrack += "/" + this.sectionTitles1[subSection];
        } else If (sectionArg == 2){
            dataToTrack += "/" + this.sectionTitles1[subSection];
        }
    getURL("javascript:urchinTracker(" + dataToTrack + ");");
    }
}

For the titlesArray I created an Array of the major sections (ones that were listed in the top level of the main navigation) so I could access them at the time the buttons were clicked by the user. There are also section titles, which were broken out into separate arrays, by each section. By leaving the first (or zero) entry blank for all of the arrays, it is easy to determine whether or not the user has clicked on a main level  menu button, or a sections sub navigation menu. It all looked like this:

var titlesArray:Array = new Array("","Home", "Portfolio", "News and Events", "FAQs", "About", "Contact");
var sectionTitles1:Array = new Array("","name","name");
var sectionTitles2:Array = new Array("","name","name");

This allows 2 variables to be passed, the main section (sectionArg), and the subsection (subSection).Then call the trackSectionData function:

this.trackSectionData(1,3);

The above function call was placed within another function which loaded external SWF files as each section was launched, but it could just as easily be placed within an on(release) on the button itself. This is pretty basic I know, but it doesn’t need to be super complicated to work.

Another option would be to utilize the External API that was introduced in Flash 8. I haven’t used it enough to make any statement as to which way is faster, but the code would be similar:

import flash.external.ExternalInterface;
send_button.addEventListener("click", clickListener);
function clickListener(eventObj:Object):Void {   
    var dataToTrack:String = this.titlesArray[sectionArg];    
    if (subSection == 0){   
        ExternalInterface.call("urchinTracker", dataToTrack);
    } else {
        if (sectionArg == 1){
            dataToTrack += "/" + this.sectionTitles1[subSection];
        } else If (sectionArg == 2){
            dataToTrack += "/" + this.sectionTitles1[subSection];
        }
    ExternalInterface.call("urchinTracker", dataToTrack); 
    }

This is enough to get you armed and ready to track all of the information you want from your users.

As a side note, you used to be able to track Flash through Mint as well, but the Flash tracking Pepper was developed for Mint 1, and is rumored to not work under the newer release. I used it a couple of times back in the day, and it worked very well – so there is hope for those who use Mint to get this sort of functionality back into their toolset – you may just have to develop your own Pepper.

2 thoughts on “Tracking Flash stats with Google Analytics”

Comments are closed.

%d bloggers like this: