Google Analytics + Flash = Easy Peasy…

You're clients will love it! And in the end you will know more about the sites you build and how to better drive traffic to certain areas! Win! Win!

googleanalytics.jpg

Ok so I have been using Google Analytics for quite sometime now and I must say i love it! So if you don't know by now Google provides FREE website statistics that go above and beyond any stat tracking systems I have seen to date. Well that is for something that is FREE. Anyway I want encourage all of you to get out there and start using Google Analytics for all your flash site going forward. Its really very simple to implement into your flash sites and even track individual events that occur in each of your sites. Please see analytics.google.com for more information on setting up your personal analytics with google and to get the Javascript needed for the code below to work. With that said let's get on to some actionscript!

googleAnalytics.as (right click - save as)

 
/// GOOGLE ANALYTICS SIMPLE TRACKING
/// Script by: corban baxter
 
import flash.external.ExternalInterface;
 
function gaTracking(page) {
	ExternalInterface.call("pageTracker._trackPageview", page);
}
 
//end

Code for simple call

 
gaTracking("/flash/home"); //sends event to google's analytics system

Now what you can understand from this is... You can call this function during ANY event function etc that you so desire. And any string combination that you want to pass to it is fair game. Use best practices when passing in the strings though so its easier for you and your clients to read when you all are reviewing the numbers. Ok so thats that! Cheers!

35 Responses to “Google Analytics + Flash = Easy Peasy…”


  1. 1 zuiopo

    damn! easy going. :)

  2. 2 Spirituc Design

    If you use SWFAddress, every path change will be registered automatically in GA :)

  3. 3 Tiago Dias
  4. 4 corbanb

    Tiago thats great! Thanks for sharing. Using External Interface with my AS2 projects opens up the door for a lot of options when making the tracking calls. Its great since using external interface won’t interfere with a normal getURL call or other similar calls out to the server.

  5. 5 Matt Przybylski

    Oddly enough, I too wrote an article about this a while back with an AS2/AS3 approach. I haven’t tested mine in the new version of Flash Player that was just released though so I’m not sure if anything will break because I wasn’t using ExternalInterface. EI is probably the better way to go so I may have to update my article. It was probably wise of you as well to wrap it into one method and just call the method, nice work.

    http://evolve.reintroducing.com/2008/01/09/tips-n-tricks/using-google-analytics-in-your-flash-projects/

  6. 6 Omar

    Using ExternalInterface is an excellent idea, as opposed to the code that Google suggest in their help pages.

    And as someone else said using SWFAddresss will automatically make page calls to GA.

    SWFAdrress + Google Analytics + Flash = Really Easy Peasy…

  7. 7 Gaston

    This post is great! Thnak you!

  8. 8 Jay

    Thanks for this solution.

    I have a question about where the Google tracking javascript code gets placed in the page that calls the SWF. On our site we have the GA script just before the …we are currently not tracking Flash events in production. I have seen a couple of blogs say that the script on these pages needs to actually be placed after the opening tag before the SWF call. Why is that? We put a test case together last night with the GA script before the close tag and the events were tracked as expected.

  9. 9 corbanb

    @Jay The code I created in this example only uses the ga.js method from google. You will have to edit it to fit the needs of the urchin tracker method if you want to use this. Thanks!

  10. 10 Dan

    Hello,

    Does this method matter how the Flash is actually being embedded in the page? I use SWFObject to embed the Flash with JavaScript.

    Will this still work?

  11. 11 corbanb

    @Dan it should not matter. I have used it with SWF Object and seen no issues.

  12. 12 Dee

    Hello,

    I’m pretty new to this so I apologize if my question is way too basic. I’m wondering where I need to be placing the “code for simple call” if it needs to be placed anywhere. Without it, my swf in FF continues to work but it isn’t working in IE. I am using SWFObject for embedding my swf with JavaScript so I’m not sure if this is interfering somehow.

    I hope someone here can help.

  13. 13 corbanb

    the code for the call to the tracking function can go just about anywhere. on a frame label. on a button. its totally up to you on where it goes in your FLA.

  14. 14 Anson Kwok

    Hi, I have a question. Where should the googleAnalytics.as code go? HTML of FLA?
    Is anyone can help me about this?

    Thanks!

  15. 15 Irene

    How would you use this with a menu generated by an array? I would like to track the main menu clicks. On the individual content pages I can track links on those pages no problem.

    Any help would be great. I have this issue with several clients since I use this form of menu for quite a few sites.

    Thanks.

    My code:

    menu_item_group.menu_item._visible = false;
    var menu_label:Array = new Array(”Home”, “About Us”, “Models”, “Castings”, “Gallery”, “News”, “Contact”);
    var total:Number = menu_label.length;
    var distance:Number = 111;
    var i:Number = 0;
    var tween_duration:Number = 1.2;

    for (; i < total; i++)
    {
    var mi = menu_item_group.menu_item.duplicateMovieClip(”menu_item” + i,i);
    mi.over = true;
    var each_substring:String = menu_label[i].split(”|”);
    mi.item_label = each_substring[0];
    mi.item_url = each_substring[1];
    mi.item_no = i;
    mi._x = i * distance;

    mi.flashmo_button.onRollOver = function()
    {
    this._parent.over = false;
    };
    mi.flashmo_button.onRollOut =
    mi.flashmo_button.onDragOut = function ()
    {
    this._parent.over = true;
    };
    mi.flashmo_button.onRelease = function()
    {
    if( this._parent.item_url != undefined )
    getURL(this._parent.item_url, “_parent”);
    else
    _root.change_page(this._parent.item_no);
    };
    mi.onEnterFrame = function()
    {
    if (this.over == true)
    this.prevFrame();
    else
    this.nextFrame();
    };
    }
    function change_page(no):Void
    {
    for (i = 0; i < total; i++)
    {
    menu_item_group["menu_item" + i].flashmo_button.enabled = true;
    menu_item_group["menu_item" + i].over = true;
    }
    menu_item_group["menu_item" + no].flashmo_button.enabled = false;
    menu_item_group["menu_item" + no].over = false;
    _root.play();
    _root.page = no + 1;
    }
    change_page(0);// the default page on load

  16. 16 corbanb

    @irene you would need to add the call to the onRelease call. You should be able to just do something like gaTracking(this.item_label);

    hope this helps

  17. 17 Peter

    Since I’m a beginner, anyone can explain me how to use Google Analytics on Myspace with this solution?

  18. 18 abhishek

    hello- was wondering if this going to work :
    gaTracking(”/flash/”+_root.variableNumber);

    where variableNumber is the dynamic number created on one page. i need to generate a dynamic page number which i would like to track.. currently i use
    getURL(”javascript:pageTracker._trackPageview(’/filetrack/1.html’);”); – which works – BUT when i try to get the number 1 dynamicly- it does not work..
    getURL(”javascript:pageTracker._trackPageview(’/filetrack/’+_root.variableNumber+’.html’);”);

    could u please help.. i have been trying to make it work for last few days……. !!!!!!!! im using AS2.

  19. 19 DasPhunk

    abhishek,

    The quotes being used within the string to insert the variable are incorrect. They need to be double quotes. Try this… getURL(”javascript:pageTracker._trackPageview(’/filetrack/”+_root.variableNumber+”.html’);”);

  20. 20 Irene

    Hi Corban -

    This has really been great for tracking my flash files. It’s easy and I am in the habit of doing all my buttons using this code. I’m still having problems with my menu array. I tried

    gaTracking(this.item_label);

    and it still isn’t registering. Any ideas?

  21. 21 corbanb

    Have you traced out “this.item_label” to make sure you are getting a string value returned. You can check the type of value in AS2 by doing trace(typeof(this.item_label)); that might be part of the reason its not firing correctly. It could also be scope when calling the function. be sure your function is in scope when you are calling it. sorry for being so late on getting back with you. enjoy!

  22. 22 Fox Jones

    I want to use GA for tracking an interactive flash banner that will be hosted on third-party sites.

    Can the SWF banner tracking work independent of any additional HTML or JS in the page it’s embedded in?

    - fox

  23. 23 Matt

    Is possible use this solution to use Google Analytics on Myspace?

  24. 24 motherbird

    COrban,

    where within the above code is the google analytics tracking code pasted?

    Do i use the simple call code or the code in the white box above or both?

    Again, where within the code is the google analytics tracking code pasted?

  25. 25 corbanb

    @matt sorry for the delay in response. but you can use the new AS3 implementation I believe on myspace widgets. check http://code.google.com/p/gaforflash/ for more info.

    @motherbird the code just needs to be placed either on a frame where you would like it to fire. or on a button event so its fired then. up to you.

  26. 26 motherbird

    corban,

    you misunderstood my question. Where within the code that you created above, do I place the google analytics tracking coade?

    Can you give an example?

    thank you

  27. 27 corbanb

    @motherbird you will get the javascript for google analytics when you signup for an account. that code must be placed in the HTML page. if you don’t want to use javascript to call the tracking you can look into the AS3 version google has developed. it can be found at http://code.google.com/p/gaforflash/

  28. 28 marcel

    Hi Corban.

    I have been struggling some time now with redirecting visitors through flash navigation to other domains, while maintaining their cookies. Maybe you could help out…

    for links using javascript the following works:

    onClick=”window.open(pageTracker._getLinkerUrl(this.href),this.target);return false;”

    in flash i have tried followin, where domain.com is our other domain which we also track within our analytics account:

    on(release){
    getURL(”javascript: pageTracker._link(’http://www.domain.com/’)”);
    }

    But this doesn’t seem to work.

    You have any idea where it goes wrong? The idea is to have visitors navigating through my domains (all tracked by same analytics account) using flash navigation, while keeping their original cookies.

    Kind regards.
    Marcel

  29. 29 Justin Rath

    @Matt – Yes you can use GA for Flash on a MySpace page. In the docs I read that to create the account you can put any string in for the URL on the google analytics page. I’m still trying to figure out how to change the status though. There is no way to put the JS on myspace and, because MySpace changes the URLS of your assets, there is no way to place any tags on the page.

  30. 30 rick

    Ok. So I want to track my MYSPACE. Ive downloaded all the AS3 files and I am able to drag the component onto my canvas in flash. I at my GA ID and select the AS3 mode. Now what?

    This seems so easy yet I cant figure it out.

    1. I have my flash file opened…what code to I copy and past into the first frame?

    2. Also how do i add my GA Script that I get from my google account?

    Thank you for your help if you can.

    Rick

  31. 31 Justin Rath

    Rick,
    Hopefully you figured it out already. I just ran into this tutorial again on accident while googling my own name!

    Number one, I would suggest getting rid of the component. I don’t know for sure but I don’t think that it ever worked for me. It also threw some errors.

    Put the “com.google.analytics” folder in the same folder as your FLA. Then you import them in your actionscript like this:

    import com.google.analytics.AnalyticsTracker;
    import com.google.analytics.GATracker;
    var tracker:AnalyticsTracker = new GATracker( this, “UA-XXXXXXX-1″, “AS3″, false );

    to create a tracking event:

    You simply use:

    tracker.trackPageview( “name of page you want tracked” );

    (the name of the page can be anything but make sure it is identifiable!)

  32. 32 Og2t

    Hey Corban, just posted an article on how to track events in SWFs embedded on remote sites: http://play.blog2t.net/tracking-events-in-embedded-swfs-with-google-analytics/ Just thought I could share.

  1. 1 How To Use Google Analytics on Myspace - Sonicko Consulting
  2. 2 Google Analytics For Adobe Flash | Google Analytics
  3. 3 Google Analytics goes AS3 | Another average flash developer

Leave a Reply






Bad Behavior has blocked 156 access attempts in the last 7 days.