AS2: GoogleAnalyticsUtils

View Documentation
Download Class

GoogleAnalyticsUtils is a utility class to easily track your clicks using Google Analytics. The class allows you to track in the old Urchin format or the new Event format as well as tracking with the old AS2 "javascript" calls or tracking with the new ExternalInterface API.

This class is a follow-up to my original post about using Google Analytics in your Flash projects.

Actionscript:
  1. import flash.external.ExternalInterface;
  2.  
  3. /**
  4. * A utility class to easily track your clicks using Google Analytics.  The class allows you to track in the old Urchin format or the new Event format
  5. * as well as tracking with the old AS2 "javascript" calls or tracking with the new ExternalInterface API.
  6. *
  7. * @author Matt Przybylski [http://www.reintroducing.com]
  8. * @version 1.0
  9. */
  10.  
  11. class com.reintroducing.utils.GoogleAnalyticsUtils
  12. {
  13. //- PRIVATE VARIABLES -------------------------------------------------------------------------------------
  14.     
  15.    
  16.    
  17. //- PUBLIC VARIABLES --------------------------------------------------------------------------------------
  18.    
  19.    
  20.    
  21. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  22.    
  23.     public function GoogleAnalyticsUtils()
  24.     {
  25.        
  26.     }
  27.    
  28. //- PRIVATE METHODS ---------------------------------------------------------------------------------------
  29.    
  30.    
  31.    
  32. //- PUBLIC METHODS ----------------------------------------------------------------------------------------
  33.    
  34.     /**
  35.      * Uses the old Urchin method in Google to track the page.
  36.      *
  37.      * @param $path The path that you want to appear in your analytics to track the selected item.
  38.      */
  39.     public static function urchinTrack($path:String):Void
  40.     {
  41.         getURL("javascript:urchinTracker('" + $path + "');");
  42.     }
  43.    
  44.     /**
  45.      * Uses the new Event method in Google to track the page.
  46.      *
  47.      * @param $path The path that you want to appear in your analytics to track the selected item.
  48.      */
  49.     public static function eventTrack($path:String):Void
  50.     {
  51.         getURL("javascript:pageTracker._trackPageview('" + $path + "');");
  52.     }
  53.    
  54.     /**
  55.      * Uses ExternalInterface and the old Urchin method in Google to track the page.
  56.      *
  57.      * @param $path The path that you want to appear in your analytics to track the selected item.
  58.      *
  59.      * @return Object The response received from the container.
  60.      */
  61.     public static function urchinEITrack($path:String):Object
  62.     {
  63.         return ExternalInterface.call("urchinTracker", $path);
  64.     }
  65.    
  66.     /**
  67.      * Uses ExternalInterface and the new Event method in Google to track the page.
  68.      *
  69.      * @param $path The path that you want to appear in your analytics to track the selected item.
  70.      *
  71.      * @return Object The response received from the container.
  72.      */
  73.     public static function eventEITrack($path:String):Object
  74.     {
  75.         return ExternalInterface.call("pageTracker._trackPageview", $path);
  76.     }
  77.    
  78. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  79.    
  80.    
  81.    
  82. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  83.    
  84.    
  85.    
  86. //- HELPERS -----------------------------------------------------------------------------------------------
  87.    
  88.     public function toString():String
  89.     {
  90.         return "com.reintroducing.utils.GoogleAnalyticsUtils";
  91.     }
  92.    
  93. //- END CLASS ---------------------------------------------------------------------------------------------
  94. }

An example usage of the class (using the Event tracking method with External Interface) would be as follows:

Actionscript:
  1. GoogleAnalyticsUtils.eventEITrack("/home/portfolio");

As you can see, the usage is very straightforward and all methods of the class are static. The path can be whatever you want it to be. This is just so that you can easily identify the path/buttons which your users clicked through when you view your stats in Google Analytics.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

1 Comment

Matt , thanks for sharing. That's exactly what I was looking for.

Leave a comment

(required)

(required)