Archive for January, 2009

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.

, ,

5 Comments


How to reuse event functions

This is just a quick tip I picked up while working with a teamate.

I often came across the case that I would have to make a function exactly the same as the function that was called from an event listener because the function called from the listener would have an event as argument but the same function that I wanted to call but not from an event didn’t.

I had found multiple ways around that problem; I would make another function that both previous function would call or I would create only the listener function and when I wanted to call it from elswhere I would pass it an empty event that I would create just for it. Both these solution appeared unelegant to me.

That’s when I saw this in my teamate code:

// let say your listener is this
myButton.addEventListener(Event.COMPLETE, _activateElement, false, 0, true);
 
//your function looks like this
private function _activateElement(event:Event = null):void{
  //do something that doesn't require the event
}
 
//so from anywhere you can just call the same function like this
_activateElement();

By simply giving a default value of null to the argument, you can now reuse this function both in the context of an event listener and anywhere else.

, ,

7 Comments


May 2009 be as good as 2008

Well, 2008 was a really good year for me, I got to work on nice projects (HomeDepot, Viagra, TELUS) and this blog’s audience grew nicely. I had an objective of getting 10k visits each month at the beginning of January 2009 and I finally got 150% percent that, so 15 000 visits. I can say that everything rolled my way nicely this year.

I know 2009 is supposed to be a tought year considering the recession and all, but I am confident that I will do well no matter what. A couple of things can explain this confidence, first I see the bunch of projects that I have lined in front of me for the comming months which will provide me work until the second half of the year. Also witnessing how much I improved this year technically speaking, I can’t imagine where I will be next year. All in all 2009 will be a good year.

So, as all starting year post I will set myself new objectives. Writting them here will give me more incentives to attain them in order to protect my pride. I guess my first objective will be to augment the attendance to this blog to 35 ooo visits a month. That is a bit bigger than doubling what I have right now. My second objective would be to write 2 – 3 articles for other publication¬† like the one I did for InsideRIA. I would also like to start looking for speaking opportunities, I don’t put to much emphasis on that one but my long term goal is to give a presentation (about topic like SEO, Google Analytics for Flash, PureMVC) in a conference somewhere in 2010. My last goal would be to look around for MBAs that I might find interesting. I always wanted to do a master degree, but since I started working that thought as been pushed back a bit, but I think next year might be a good year to go back to school; at least part time.

I also have some personal objectives like going to the gym more often and eating better, but that is of lesser interest to this blog.

I hope you all get a good 2009 year and that the recession does not darken to much what you see for yourself in the coming year.

, , , , ,

3 Comments