Using Google Analytics for Flash in conjunction with PureMVC

I have been playing lately with Google Analytics for Flash and since my previous attention span (I noticed than I tend to write a lot on a topic until I find something else that interest me a lot, now is Google Analytics’ turn)  was spent on PureMVC, I thought it would be good too write a post about using the two of them together.

It is actually very easy to integrate them. All you have to do is dedicate a Proxy to Google Analytics. First go download Google Analytics for Flash (I’ll write a post later on how to import it because I found several ways). From there just create a Proxy for it like this:

package com.zedia.model{
  import flash.display.DisplayObject;
  import org.puremvc.as3.interfaces.IProxy;
  import org.puremvc.as3.patterns.proxy.Proxy;
 
  import com.google.analytics.AnalyticsTracker;
  import com.google.analytics.GATracker;
 
  public class GoogleAnalyticsProxy extends Proxy implements IProxy{
    public static const NAME:String = "GoogleAnalyticsProxy";
 
    private var _tracker:AnalyticsTracker;
 
    public function GoogleAnalyticsProxy(main:DisplayObject){
      super(NAME);
 
      _tracker = new GATracker( main, "UA-639032-8", "AS3", false );
      _tracker.trackPageview();
    }
 
    public function trackAction(action:String):void {
      _tracker.trackEvent( "my category", action);
    }
 
    public function trackLabel(action:String, label:String):void {
      _tracker.trackEvent( "my category", action, label);
    }
  }
}

What is cool with this Proxy is that it is tied with nothing, you can just go and use it in any project and all you have to do is change the package name. Also notice that for the category argument of the trackAction and trackLabel method I used the “my category” String. I live in Montreal in Canada and we have here two official languages: French and English. I have found that putting the language as a category was a good habit, but I think it is not the case for every one, so I left it as a generic String in this example.

The next thing you have to do is to register this Proxy. The best place to do so is in the Startup Command. You do so this way:

facade.registerProxy(new GoogleAnalyticsProxy(Object(note.getBody()).main as Main));

Google Analytics for Flash require a DisplayObject as his first parameter and the Startup command usually needs a reference to the Stage to pass to the ApplicationMediator. In this case the main in the notification will serve me both as a link to the Stage and as a DisplayObject.

Now we are all set, we can call upon this Proxy from anywhere in our PureMVC application. I would suggest making a Command for just that. Here is the code you would put in your Command:

//retireve the Proxy
var googleTracker:GoogleAnalyticsProxy = facade.retrieveProxy (GoogleAnalyticsProxy.NAME) as GoogleAnalyticsProxy;
 
//Use tracking with just an Action
googleTracker.trackAction("Send to Friend");
 
//Use tracking with Action and Label
googleTracker.trackLabel("Error", "Send to Friend - Incomplete form");

Well it is that easy to integrate Google Analytics for Flash with PureMVC.

, ,

  1. #1 by Kris - March 5th, 2009 at 00:31

    Your blog is great. Keep it up.

    This post makes me want to start using pureMVC. Beautifully easy.

  2. #2 by Gil - March 5th, 2009 at 02:09

    Cool. Also… if you use SWFAddress then analytics support is built-in.

  3. #3 by zedia.net - March 5th, 2009 at 11:51

    @ Gil
    SWFAddress only sends pageViews, if you want to send Events then this is useful.

  4. #4 by Marc - March 12th, 2009 at 06:47

    Hi,

    I’ve just finished a Flex app using pureMVC and was about to add Google Analytics metrics to it. I’ve found your blog and this note just in time!
    I’ve created a proxy like you did, registered it in my facade and created two commands which are registered to notifications.
    The snag is that is just won’t work. :-(
    I get a “TypeError: Error #1009: Cannot access a property or method of a null object reference” when in my proxy I do:
    _tracker = new GATracker( main, MetricsConstants.ACCOUNT, MetricsConstants.MODE, MetricsConstants.DEBUG );
    The “main” isn’t null, I’ve looked with the debugger, it is an instance of my app.

    Any clue?

    Thanks!

(will not be published)
Subscribe to comments feed