Integrate Google Analytics with Flash | Tutorial

Google Buzz

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.

UPDATE

I now have an Event Tracking Tutorial as well with actionscript updates!
Event Tracking With Google Analytics & Flash/Actionscript Integration Tutorial

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

Get Adobe Flash player

track Google Analytics actionscript function

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

Calls to Google Analytics actionscript function

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

New Event Tracking technique tutorial rather than posting each event you want to track in flash as a pageview it can be a specific event!

  • del.icio.us
  • Digg
  • email
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • Hexosearch
  • LinkedIn
  • Mixx
  • Print
  • PDF
  • StumbleUpon
  • Technorati
  • Twitter
  • RSS
This entry was posted in tutorial and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

40 Comments

  1. somnamblst
    Posted 31 January 2008 at 10:05 am | Permalink

    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?

  2. Posted 31 January 2008 at 11:17 am | Permalink

    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!

  3. Posted 9 February 2008 at 5:58 am | Permalink

    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…

  4. Kathy
    Posted 26 March 2008 at 9:52 pm | Permalink

    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

  5. Andy
    Posted 11 July 2008 at 11:51 am | Permalink

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

  6. Andy
    Posted 11 July 2008 at 11:52 am | Permalink

    because the ball…bounces.

  7. Posted 23 July 2008 at 11:52 am | Permalink

    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.

  8. Posted 1 September 2008 at 3:53 pm | Permalink

    very nice :)

  9. Posted 4 September 2008 at 2:35 pm | Permalink

    very usefull, many thanks !!
    it works like a charm :)

  10. Posted 21 September 2008 at 2:18 pm | Permalink

    Excellent tutorial. I have been reading Advanced Web Metrics for the past few days. There is a small section on flash event tracking. It is somewhat informative. But your tutorial is better. Now I just have to piece together how to use if for a video.

    I am trying to track the clicks on Play, Pause, and time to download the entire video. This would be extremely helpful to track how people are viewing the video with different band widths as well as figuring out if they are viewing the entire video.

    Great tutorial…thanks.

  11. lolo
    Posted 4 October 2008 at 7:41 am | Permalink

    any question, how it could work in a Flash Air application like a widget, because I dont know if Air stuff embede Java and HTML with ja code?

  12. Posted 13 October 2008 at 7:53 am | Permalink

    Hi there,

    Great post, very helpful.
    I have had to slightly change your code to make it a JS function, and then call the JS function from my swf file in order to track a click. Ran into this problem below tho:

    http://www.moock.org/asdg/technotes/crossDomainPolicyFiles/

    Problem with cross domain security!!

  13. Posted 13 November 2008 at 2:03 pm | Permalink

    This would be absolutely great if I knew what you were talking about.

    The minute you started refering event tracking I was lost.

    You have an .swf which has buttons
    Those buttons click through for pages
    You want to track click throughs
    So you are placing the tracking code in the button layer movie’s actions layer?

    I feel like I need to go back about 4 steps.

  14. Posted 13 November 2008 at 2:36 pm | Permalink

    @Avangelist – This is a tutorial to track an event (something that happens in Flash) in Google Analytics as a pageview.

    This is a simple example about how to implement this idea. It can be pushed much further. This example shows how to track events with this ball (user interaction and not) in Google analytics. If we wanted to havea button going to another page it may be better to just put the analytics code on that page.

    You have a swf with buttons, which click through for pages? are these “pages” separate html pages or frames in your flash?
    You want to track the click of the button? That would be the exact way I track clicking this ball in the example. I’m placing the tracking code in the onPress event for that movieClip, as you can see in the source file provided.

    _root.ball.onPress = function () {
    startDrag(this);
    this.dragging = true;
    trackGA(“ball/”+this.ballNum+”/pressed/”);
    }

    thanks for reading

  15. Posted 16 November 2008 at 10:12 am | Permalink

    Hi,

    I tried this code, but in my Google Analytics page I don’t see any separate entries for the events that I created. Do I need to create some corresponding entries in GA settings manually too?

    Thanks,
    Mukesh

  16. Posted 16 November 2008 at 10:52 pm | Permalink

    Mukesh – Nope, it should show up, be sure to give about 24 hours for the hits to show up in your reports. But in this example I make calls to

    1
    trackGA(”ball/”+this.ballNum+”/pressed/”);

    , so you can search in the top content page of your reports for a page containing (in this example) “ball/” or “/pressed/” and if everything happened correctly they should show up there.

    If you aren’t as patient as that though, you can see live evidence of the tracking being sent to google analytics… Get the firebug extension for firefox and you can easily monitor network calls. This will show you the header and parameters sent with all calls to google analytics, you can see the path that gets sent! Pretty cool, eh? It will look like this “GET __utm.gif?utmwv=……sending tons of parameteres!

  17. Posted 17 November 2008 at 9:58 am | Permalink

    Thanks Evan. Actually I had waited for 48 hours before contacting you. What you suggested about Firebug sounds cool. That will help me in finding if I am making any syntactical or other mistake.

    Thanks,
    Mukesh

  18. Loony
    Posted 18 November 2008 at 7:54 am | Permalink

    Hi.
    Thanks for all the info.
    My website is all flash (swishmax to be precised).
    I have a menu with several buttons. Once a button is released the movie goes to a frame and plays it. How do I add google analytics to track that somebody clicked that button?
    Here is the simple code – but unfortunately no answer:

    on (release) {
    _root.gotoAndPlay(66);
    }

    Please make it real simple! Thank you

  19. Posted 18 November 2008 at 8:22 am | Permalink

    @Looney – try this:

    on (release) {
    traceGA(“buttonRelease/66″);
    _root.gotoAndPlay(66);
    }

    function trackGA(action:String) {
    //Old Google Analytics Code
    ExternalInterface.call(“urchinTracker(‘/”+action+”‘)”);
    //New Google Analytics Code
    ExternalInterface.call(“pageTracker._trackPageview(‘/”+action+”‘)”);
    trace(“Google Analytics Tracking: ” + action);
    }

    You can send a different action on each button, and analytics will tell you reports for each button pressed. Or you could put the analytics call onto the timeline, so when they go to a certain frame the function is called to log the tracking.

  20. Loony
    Posted 19 November 2008 at 3:56 am | Permalink

    wow thank you so much for being so quick at replying. I’m trying it now.

  21. Loony
    Posted 19 November 2008 at 4:11 am | Permalink

    There seems to be a problem with the line:
    traceGA(“buttonRelease/66″); It appears yellow meaning there is an error.

    Here is what I have once I added the code you gave to me:
    on (release) {
    traceGA(”buttonRelease/66″);
    _root.gotoAndPlay(66);
    }

    function trackGA(action:String) {
    ExternalInterface.call(”pageTracker._trackPageview(’/”+action+”‘)”);
    trace(”Google Analytics Tracking: ” + action);
    }

    Now the button is inactive. Any idea?

  22. Posted 19 November 2008 at 10:39 am | Permalink

    @Loony – if you look closely you’ll see the type… traceGA != trackGA, my bad.

  23. Loony
    Posted 24 November 2008 at 4:55 am | Permalink

    There still seems to be a problem. But thank you very much anyway ; I appreciate your time.

  24. abhishek
    Posted 12 December 2008 at 11:36 am | Permalink

    Im using AS2 and with getURL method..
    getURL(“javascript:pageTracker._trackPageview(‘/filetrack/1.html’);”);
    - it does work but when i try to get the page number dynamically..
    getURL(“javascript:pageTracker._trackPageview(‘/filetrack/’+_root.pageNumber+’.html’);”); IT DOES NOT WORK.. someone please help where im doing wrong- i can however trace the root.pageNumber and it shows the page number right but for some reason.. it does not work- please help.

  25. Posted 16 December 2008 at 1:29 pm | Permalink

    i used your example and it’s not working for some reason… although i don’t understand why you’re passing the javascript function’s parameters all within the contents of the ExternalInterface.call’s first parameter, instead of:

    ExternalInterface.call(“pageTracker._trackPageview”, action);

  26. Posted 21 January 2009 at 9:10 am | Permalink

    Abhishek, you forgot to add 2 double quotes, this should work:

    getURL(“javascript:pageTracker._trackPageview(‘/filetrack/’”+_root.pageNumber+”‘.html’);”);

  27. Posted 23 February 2009 at 3:47 pm | Permalink

    By placing the Google tracking script in the html file that calls up the .swf file, will that work properly? Right now Google will read the HTML page, but none of the flash functions. I used some of your code and everything worked ok in Flash.
    thanks

  28. Posted 2 March 2009 at 12:46 am | Permalink

    if i use your sample then its work?

    dude i really stuck between google analytics and flash plz help me out

  29. Posted 2 March 2009 at 1:06 am | Permalink

    Dear Evan Mullins,

    now i upload ur example file and setup it for google analytics on http://www.dblueroom.com my orignal site address is http://www.dblueroom.com/_final_index.htm could u pleas check it and wat is next step ?

  30. Posted 4 March 2009 at 12:11 am | Permalink

    yes yes yes its working wow thx dear thx alot :D

  31. Posted 11 March 2009 at 10:11 am | Permalink

    Great to hear you got it all working Fahad,
    let me know how it works for you.

  32. Posted 9 April 2009 at 11:34 am | Permalink

    If you’re having trouble accessing the reports simply hack the URL to include the events folder and away you go http://www.leewoodman.co.uk/blog/09042009/346752/google-event-tracking-hack/

  33. michy
    Posted 27 May 2009 at 1:45 am | Permalink

    In google analytics can u show me way how to embed google analytics in a media player(for example: JW player)

  34. Posted 12 June 2009 at 6:35 pm | Permalink

    Hey there, 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.

  35. Posted 29 October 2009 at 9:39 am | Permalink

    I’m using a single Flash button to play multiple audio files,. So I just update the HTML to reflect the audio file I want played. However I want to track each audio file seperately. Can I track the clicks on the play button, in Analytics, without updating the Flash file?

    Web page: http://www.lyonspr.com/listen.php

    Is there Javascript I can use instead? Any ideas?

  36. DavidF
    Posted 4 December 2009 at 2:53 pm | Permalink

    this is for an Advent calendar .. I need to drop some analytics into a Flash movie .. using a flash file as an index which has 24 buttons .. could I put the analytics on the index to show which buttons are being clicked .. or each of the 24 flash movies it launches.

    I’m not quite sure how the 24 flash files are being accessed (as it’s behind a corporate firewall, so I cannot see it) an I’m not so much of a flash expert (you guessed that didn’t you!) .. can the analytics code be put directly into the flash move somehow without any need to drop anything into an HTML page?

  37. Posted 7 December 2009 at 10:19 am | Permalink

    @ Greg Johnson – I’d think you have to update the flash file and include the script to communicate with analytics. You could use an html button pretty easily and do all the control with javascript as well, not sure what functionality you have wrapped up in flash though. Good Luck!

    @DavidF – to do the flash google analytics communication you shouldn’t need to access the html as long as it has google analytics properly installed. (Keep in mind that if it’s not your google analytics tracking code account number you wont have access to the analytics though). If I understand correctly, I’d say put the tracking code in your flash index file and each time you click one of the 24 buttons, send a call to google analytics.

  38. Jay
    Posted 15 December 2009 at 4:33 pm | Permalink

    So my question is…if I wanted to track… for example, if I run this banner (http://mercl.com/glucocil/banners/gluc_160×600.html) on http://www.WebMD.com.

    I’m not the guy putting the code on WebMD.com so is there a way to get the code all together so what I send him can be placed easily?

    Not sure if I’m making sense. I know on my own page I could put the code wherever I want, but counting on someone else…when you’re paying alot…another issue.

  39. Shane
    Posted 6 January 2010 at 10:09 pm | Permalink

    I am trying to track when someone watches a flash movie on my site I have used the following code on the play button but I don’t see any results in GA it has been over 24 hours since I have added the code and put it on the site. the rest of GA is updated but it does not show the video anywhere.

    on (release) {
    //Track with action
    getURL(“javascript:pageTracker._trackPageview(‘/video/getDeal.php’);”);
    _root.gotoAndPlay(4);
    myVar = “Video Play From GetDeal”;
    }

  40. Jonathon P
    Posted 22 February 2010 at 8:15 am | Permalink

    I’ve done everything in your example ….. nothing is getting recorded to GA. Only the page hits seem to get recorded.

    Can you be more specific with this part

    ” (Note that your analytics tracking code must be placed on the page above the flash call(s) to _trackPageview or urchinTracker)”

    My GA code that is in my HTML file is sitting right above my closing BODY tag.

    I have tried putting the code right above my Flash embed statement too … still doesnt work.

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>