Skip over navigation

Google Analytics Event Tracking in Flash

By on Jun 4, 2010 in Tutorials, Web Engineering

So, I needed to add some Google Analytics even tracking to a Flash banner today. So, I think to myself, “It’s just a simple JavaScript call. No problem.”

pageTracker._trackEvent(
   'flashBanner',
   'changeTab',
   'newTabName'
);

In ActionScript, you can use the getURL() function to make JavaScript calls, like so:
getURL(“javascript:alert(‘Where are my pants!?!’);”)
So, it would follow that I could do this:

getURL("javascript:
   pageTracker._trackEvent(
      'flashBanner',
      'changeTab',
      'newTabName'
   );
");

Well, as an Action Script novice-to-intermediate, I didn’t realize there would be a problem. Turns out that the _trackEvent() returns a boolean value. It also turns out that getURL() will take any value it’s given and run with it. So, when I installed the banner and switched tabs, my browser left the page. In the address bar, was the JavaScript call I made, and the page just said, “true”.
I needed to muffle the return value from _trackEvent() so that getURL() would sit quietly. So, I wrapped the _trackEvent() call in an self-invoking anonymous JavaScript function.

getURL("javascript:
   (function() {
      pageTracker._trackEvent(
         'flashBanner',
         'changeTab',
         'newTabName'
      );
   })();
");

This created a new function, that did not have a return value to hand to getURL(). Inside, it calls _trackEvent() for me, and throws away that pesky boolean. I put this all in the ActionScript of my tab button, to be called on mouse press.

on(press) {
   getURL("javascript:
      (function() {
         pageTracker._trackEvent(
            'flashBanner',
            'changeTab',
            'newTabName'
         );
      })();
   ");
}

Now I can track events all day in peace.
P.S. – I’ve put all my example code above across several lines, and added indentation to make the more readable. But in my ActionScript, it’s done all on one line.

Hey! This wasn't written by a leash of foxes! It was written by , who does awesome work at Loud Dog, a digital branding firm in San Francisco that helps businesses express themselves authentically via identities, websites, and marketing collateral.

If you want us to do awesome work for you, if you have a question, or if you're just feeling lonely and want to chat, we want to hear from you!

Get in touch

Leave a Reply

Your email address will not be published. Required fields are marked *