Event Tracking with Google Analytics | Flash Integration | Tutorial

    Many have read my Integrate Google Analytics with Flash Tutorial in which I express enthusiasm for the new event tracking at google analytics! Well, it’s been a while, but I was admitted to the Beta testing group! So I’ve now had the chance to play with event tracking a bit and wanted to publish my findings!

    Overview

    Almost a year ago Google Analytics announced their new event tracking model and have had help documents published and code samples up. And as with many of Google’s products the beta stamp has lasted a very very long time. Many have seen my earlier tutorial exploring using traditional Google Analytics Tracking from within Flash, and it does wonders to track your flash apps in this manner, but there is a problem with it. We’re using supposed object oriented concepts to track objects as pageviews. One thing is it really isn’t a very intuitive way to represent that data, and another it inflates your pageviews! The solution? the long awaited and announced Event Tracking model. I’ve been itching for this to be released so I could refresh my analytic tactics I use in my flash projects. No, to answer your questions, it has not been released yet, but I contacted Google and explained that I would be a great beta tester for this feature and after a bit of correspondence they invited me to join in the beta testing! This is good news for you too! Because I will tell you all about how to do it and even show you what the reporting looks like and when it is released finally, you will know what you’re in for after this sneak peak!

    UPDATE: Here are the reports for this very example: Report from Event Tracking with Flash Tutorial

    The very quick summary is this:

    _trackEvent(category, action, optional_label, optional_value)

    Note that the _trackEvent function is called on the pageTracker object itself. (initially Google had you instantiate a separate event tracker for every object (or category) you wanted tracked)

    For example, if we want to track a ball. All the actions that can apply to the ball are: it being created, dragged, dropped, bounced, deleted… You get the idea. We can have direct user actions tracked or even automatic actions. If we have gravity and physics running, the ball may bounce a lot without any direct user interaction. But it will never be dragged or dropped without direct interaction. I’d recommend only tracking user interactions because who cares how often a ball bounces on your page (unless you’re doing an experiment, of course), want we want to know is how and when a user interacts with the ball.

    category:string (required)

    This is the name of the object you are tracking.

    action:string (required)

    This is the action that happens to your object you want to track.

    optional_label:string (optional)

    This can be more information to accompany the action.

    optional_value:integer (optional)

    A number to provide numerical information to accompany the action.

    Steps

    1. First, I’d recommend reading up about Event Tracking at Google
    2. Decide your object oriented structure for tracking events. What objects do you want to track and what useful information do you want to get through tracking user interaction?
    3. Make sure you have the new Google analytics tracking code on your page
    4. Use these functions to communicate Google Analytics from your flash
      1. Call the main function with the specified parameters
      2. It will call the appropriate function and send the data to your pageTracker object through javascript with externalInterface calls
    5. See the reports in your analytics profile! (if your a beta tester, or else, wait until it is released)

    Source code

    The tracking functions are below, I enhanced the earlier trackGA function I wrote about. Now you call trackGA with 2 required parameters, categoryOrPageTrack and action. categoryOrPageTrack is where you have to pay attention. I wanted to keep the ability to track pageviews as well as have event tracking, so as the first param you either send in the string ‘page’ to explicitly state that you want to track the page view, or you send in another string to state you want to track an event on that specified object. Action remains the same, the action you want tracked (either on the pageview, it is the path that will appear in your reports; or the event tracking will be the action tracked to the category)…
    So to track a pageview I call

    trackGA("page", "swfLoaded");

    and to track an event to an object I call ball:

    trackGA("ball", "created");

    The trackGA function will rout your call to the appropriate place and send the info to Google through either the trackGAPage function or the trackGAEvent function.

    //trackGA (categoryOrPageTrack [required], action [required], label [optional], value [optional]
    //categoryOrPageTrack - either the category string or a string saying 'page'
    function trackGA(categoryOrPageTrack:String, action:String, optional_label:String, optional_value:Number) {
    //call page tracking version of Google analytics
    if (categoryOrPageTrack == "page") {
    //trace("GATC pageTracker call");
    trackGAPage(action);
    }
    //call event tracking method
    else {
    //trace("GATC event tracker call");
    trackGAEvent(categoryOrPageTrack, action, optional_label, optional_value);
    }
    }

    var prefix:String = "flashGA";
    //Google Analytics Calls Page Tracking - for tracking page views
    function trackGAPage(action:String) {
    //GA call
    if (prefix != null && !eventTrack){
    var call = "/" + prefix + "/" + action;
    //Old Google Analytics Code (urchinTracker)
    ExternalInterface.call("urchinTracker('"+call+"')");
    //New Google Analytics Code (_trackPageview) pageview
    ExternalInterface.call("pageTracker._trackPageview('"+call+"')");
    trace("==GATC==pageTracker._trackPageview('"+call+"')");
    }
    _root.tracer.text = action;
    }

    //Google Analytics Event Tracking Calls - for tracking events and not pageviews
    //category, action, label (optional), value(optional)
    function trackGAEvent(category:String, action:String,  optional_label:String, optional_value:Number) {
    /*
    objectTracker_trackEvent(category, action, optional_label, optional_value)
    category (required) - The name you supply for the group of objects you want to track.
    action (required) - A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
    label (optional) - An optional string to provide additional dimensions to the event data.
    value (optional) - An optional integer that you can use to provide numerical data about the user event.
    */


    theCategory = "'" + category;
    theAction = "', '" + action + "'";
    theLabel = (optional_label == null) ? "" : ", '" + optional_label + "'";
    theValue = (optional_value == null) ? "" : ", " + optional_value;
    //New Google Analytics Code (_trackEvent) event tracking
    theCall = "pageTracker._trackEvent(" + theCategory + theAction + theLabel + theValue + ")";
    ExternalInterface.call(theCall);
    trace("====GATC===="+theCall);
    _root.tracer.text = theCategory + theAction + theLabel + theValue;
    }

    Here’s the actionscript lines where I call the trackGA function:

    //Tracks that the swf loads, so I pass 'page' to let it know I want a pageview tracked...
    trackGA("page", "swfLoaded");
    //Tracks various objects sending various actions
    trackGA("gravity", "on");
    trackGA("gravity", "off");
    trackGA("friction", "on");
    trackGA("friction", "off");
    trackGA("ball", "deleted", count);
    trackGA("ball", "created", ballCount);
    trackGA("ball", "drag", this.ballNum, this.ballNum);
    trackGA("ball", "drop", this.ballNum, this.ballNum);
    trackGA("ball", "bounce", "right", this.ballNum);

    Example

    Get Adobe Flash player

    View example in it’s own html page, I even added a couple html buttons with javascript hooked in to show javascript event tracking implementation.

    Download

    Download Source

    Concerns

    I’ve noticed while putting this together that the calls to google analytics are not completely fullfilled, this example sends out correct calls to javascript, but (in firefox at least) a max of about 1 tracking call is registered with the tracking code every 5 seconds or so. I noticed this as I was monitoring the drag and drop events for each ball, although the drag and drop events are both fired, usually the drag event was received and the drop is not. After verifying that my code was consistent, I noticed that no matter how fast I interacted with the objects, the calls were much slower. I’m guessing this is a limit placed by the google team to keep us from sending pointless data such as is posted at the bottom of the event tracking implementation guide, titled Events Per Session Limit.

    This entry was posted in tutorial and tagged , , , , , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    25 Comments

    1. lolo
      Posted October 3, 2008 at 2:55 pm | Permalink

      Great Thanks, I go to study all this great stuff

    2. lolo
      Posted October 4, 2008 at 7:42 am | Permalink

      it could work through a widget via Adobe Air runtime?

    3. Mukesh
      Posted October 16, 2008 at 12:05 pm | Permalink

      Hi,

      Your code seems quite helpful. I put it in my flash application to track some events.

      But those events don’t seem to appear in Google Analytics page for my website. I was checking on net and somewhere read that the event name should begin with a “/” . But that doesn’t seem to be the case in the example that you wrote. What is correct?

      Also, after I change my flash code, do I need to go and make some changes to my analytics settings too? e.g. adding some goals or maybe something else?

      Please let me know.

      Thanks,
      Mukesh

    4. Posted October 17, 2008 at 7:19 am | Permalink

      @Mukesh – Event tracking is still in BETA mode, therefore those who are not beta testers are not given tracking for events.

      You must have the new google analytics code on your site.

      Beginning the event with a backslash? Haven’t heard of that, but as you can see, my events showed up just fine in my reports (because I’m a BETA tester). I haven’t checked to be sure, but I think you can have it all set up and sending events to your analytics correctly and google will record them but not show them to you until they turn event tracking on… Meaning you should be able to watch the http requests or the Network monitor in firebug and see the call go out to analytics.

    5. Mukesh
      Posted October 17, 2008 at 11:07 am | Permalink

      Oh it is still in beta mode. I didn’t catch that from ur post :(

      So what is the best a flash coder can do right now to track activities on flash page?

    6. Mukesh
      Posted October 17, 2008 at 11:35 am | Permalink

      Sorry I should have read carefully. I thought as beta tester u have the results immediately while others have to wait for 24 hrs to check.

    7. Posted October 18, 2008 at 10:06 am | Permalink

      My Integrate Google Analytics with Flash Tutorial I show an available way to use analytics from within flash with a working example and source. Enjoy!

    8. Mukesh
      Posted October 19, 2008 at 10:42 am | Permalink

      Thanks. I’ll check that.

    9. Posted November 24, 2008 at 9:50 am | Permalink

      Hey wazzup bro! Great job with this tutorial . It s awesome and very helpful. I have a question. If you go to my website http://www.react-magazine.net and you go to the bookcase page (sorry not english yet and still in beta stage of design :) ), you can see there some links from three photographs, another one from a postcard and two more hidden in the magazine of the middle of the page.

      Well, using your tutorial I am able to track every single click in the photos links but it doesn t work for the “download the magazine” buttons (the ones hidden in the magazine). Does it make any sense or am I doing something wrong?

      I have seen that the code for tracking the download events in HTML is a little diferent. Should I edit the “logging.as” file with that code?

      Thanks man, agan brilliant work!

    10. Posted November 26, 2008 at 5:30 pm | Permalink

      hey man wazzup, it s me again :) just two questions:

      1. I downloaded the firefox plugging to check the web transit and defenetly my download links doesn t send any kind of signal to Google Analytics. DO you now what to do then with that?

      2. Please dont forget me! :) I have been checking your post since I wrote waiting for your answer. Hehe, just kidding, I ll seriously appreciate your help :)

      Thank you! Take care

    11. Posted December 1, 2008 at 3:42 pm | Permalink

      If you’re not seeing anything in firebug going to google analytics that would mean your problem is in the code. Are those download buttons html text or movieclips? Html text if handled differently than a movieClip click. Good luck

    12. Posted December 1, 2008 at 4:56 pm | Permalink

      hey wazzup
      thanks for answering :)
      they are simple button symbols made in flash with the getURL action script. They are linked to the file in my server so clicking them you download it.

      As I told you, I followed the steps of your tutorial and it doesn t track the click. I have been able to track the rest of events of the web, also those getURL events that are linked to other sites

      Should those buttons work as well as the other events? am I doing something wrong? Have you checked that?

      PS: I m so sorry for my precarious english :)
      PS2: Don t wish me good luck yet :( . It seems that you will never answer me again. Don t leave me aloneeee XD

    13. tobo
      Posted January 7, 2009 at 12:16 pm | Permalink

      Hi, This is great, but any chance of an AS3 version?
      Thanks

    14. Vladimir Martins
      Posted June 2, 2009 at 5:15 pm | Permalink

      Hi there,
      I’ve got a flash player (AS2) and I offer my clients a choice of embed. like whoever is watching the video can embed it on their facebook, blog, etc.
      Do you know if it is possible for me to have analytics tracking where and how many times an event is happening (like how many times the loader runs)? So I could have an idea about how the pre-rolls and post-rolls are behaving.
      Could you please get in touch with me via email so we could discuss it?

      Cheers,

      Vlad

      • Posted June 4, 2009 at 1:34 pm | Permalink

        @Vlad – You can have analytics track most anything that can be measured. Although if you are letting people embed the flash onto their site, then the analytics will go to their account. Analytics on the page will work if and only if there is analytics tracking code in that html file, so if you’re wanting to track your file across other sites, I don’t think it’s possible with this method. Does that answer your question?

    15. Posted June 10, 2009 at 11:02 am | Permalink

      Vlad – I had the same issue. I created an AS3 shell to load the AS2 player. Then I placed a button on the AS3 stage that tells me whenever the video is clicked. Not perfect, but it seems to be working. I get good tracking.

      However, I’m trying to figure out how to track which URLs are using my code. Anybody have any ideas?

    16. Posted August 31, 2009 at 4:10 pm | Permalink

      Hi Evan,

      I’m looking to integrate GA with my Flash AS 2 file.
      The index.swf opens ‘pages’ (external swfs) on the release of a movieclip.

      Could you lend me a hand as to the path I should take to accomplish this. I’ve read up on multiple tutorials referencing this integration and can’t for the life of me get it to work.
      Thanks so much.

      • Posted September 2, 2009 at 12:16 am | Permalink

        @Mark
        Have the index swf call to google analytics with external interface every time it loads another ‘page’ or swf.

    17. Eliza
      Posted November 1, 2009 at 6:52 am | Permalink

      How to track number of clicks for an event?

      is it just a matter of using the 3rd optional_value in the function call or is it necessary to customize a function like the ones in ‘ball.as’ ?

      e.g.
      function createNewBall() {
      //if (ballCount < maxBalls) { //commented out this to focus on the ++ loop but left it in so other readers could identify it.
      ballCount++;
      }

    18. Eliza
      Posted November 1, 2009 at 2:44 pm | Permalink

      Another question:
      When I use the tracking code function calling page view ( ‘page’) in the root time line it sends data to GA.

      However, I tried using the event function call code in a frame nested within the timeline of a movie clip and I am Not getting any data sent.

      Is there a way of calling an event in a nested time line?

      Should I try calling the function off a button click as opposed to when the playhead reaches a frame?

    19. Posted December 11, 2009 at 1:19 am | Permalink

      I want to track flash banner on my site, please tell me what i need to insert

      _trackEvent(category, action, optional_label, optional_value)
      or
      pageTracker._trackEvent(category, action, optional_label, optional_value)

      please

    20. Posted July 7, 2010 at 10:33 am | Permalink

      Hey Man,
      you did a nice job!!!

      Does anbody know how It works wit AS3?

    21. Beta
      Posted August 15, 2010 at 3:04 am | Permalink

      Simple and nice tracking…with google analytics, thanks for tutorial.

    22. Singh
      Posted July 7, 2011 at 3:17 am | Permalink

      only three categories are displaying,but i have given four categories,but it is showing three categories in the overview.
      please let me know where it is wrong.

      Thanks

    One Trackback

    1. [...] on the site through Google Analytics as well. In this effort we refer the method introduced by Evan Mullins to integrate the event tracking functionality of Google Analytics to the Flash application. I have [...]

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

      CSS3 Button/Icon set

      I've been secretly hoping to see a few of these pop up once the whole icon font idea spread through the nets. I really like this idea and it's a very nice implementation too! I only see some quality issues on a couple of the icons (such as youtube), but it's awesome and I hope [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...