Source for Sample Google Flash Tracking Analytics SWF

Applies to Roles
Developer
Version
Brightcove 4
Modules
Player API
Edition
Express, Pro, Enterprise

This page presents the source for the example Google Flash Tracking Analytics SWF, GoogleAnalytics.as. You can also download it as a zip file. This sample file fires the player load, stream start, and stream complete events to Google Analytics. It is described in more detail in Sample Analytics SWF: Google Analytics Flash Tracking.

package {
     import com.brightcove.api.APIModules;
     import com.brightcove.api.CustomModule;
     import com.brightcove.api.events.ExperienceEvent;
     import com.brightcove.api.events.MediaEvent;
     import com.brightcove.api.modules.ExperienceModule;
     import com.brightcove.api.modules.MenuModule;
     import com.brightcove.api.modules.VideoPlayerModule;
     import com.google.analytics.AnalyticsTracker; 
     import com.google.analytics.GATracker; 
     import flash.display.Stage;
     import flash.system.Security;
     
     public class GoogleAnalytics extends CustomModule {
     // ----------------------------------------------------------------------------
     // Constants
     //Your Google Account ID is loaded from the URL parameter gid
      private const ACCOUNT_ID     : String = loaderInfo.parameters.gid; 
      private const BRIDGE_MODE    : String = "AS3";
      private const DEBUG_MODE     : Boolean = false;
     // ----------------------------------------------------------------------------
     // Event Names
      private const EVENT_PLAYER_LOAD    : String = "player_load";
      private const EVENT_VIDEO_START    : String = "video_start";
      private const EVENT_VIDEO_COMPLETED: String = "video_complete";
     // ----------------------------------------------------------------------------
        
      private var _bcExperience:ExperienceModule;
      private var _bcVideo:VideoPlayerModule;
      //private var _bcMenu:MenuModule;
      private var _bcStage:Stage;
      private var _tracker:AnalyticsTracker;
  
      /**
      * Constructor.
      */
      public function GoogleAnalytics() {
      Security.allowDomain("*");
      }
  
      // ----------------------------------------------------------------------------
      /**
      * Fire an event.  This method has all the logic with respect to 
      * how exactly to fire the event, as a page load or as a link 
      * click.
      */
      private function fireEvent(eventName:String):void {
      var experienceId:Number  = _bcExperience.getExperienceID();
      var playerName:String  = _bcExperience.getPlayerName();
      var action:String = "";
      switch (eventName) {
      case EVENT_PLAYER_LOAD:
      var experienceURL:String = _bcExperience.getExperienceURL();
      var referrerURL:String = _bcExperience.getReferrerURL();
      action = "/playerid=" + experienceId + "/playername="
         + playerName + "/url=" + experienceURL + "/refurl="
         + referrerURL + "/" + eventName;
      break;
      // add additional cases here if needed
      default:
      var video:Object = _bcVideo.getCurrentVideo();
      var playlistId:Number  = video.lineupId;
      var videoId:Number   = video.id;
      var videoName:String = video.displayName;
      action = "/playerid=" + experienceId + "/playername=" + playerName 
        + "/playlistid=" + playlistId + "/videoid=" + videoId
        + "/videoname=" + videoName + "/" + eventName;
      }
     _tracker.trackPageview(action);
      }
     /**
      *  Register for all interesting events here.
      */
      private function registerEvents():void {
      _bcStage = _bcExperience.getStage();
      // Create a Google tracker with reference to the Brightcove player stage and your Google Account ID
      _tracker = new GATracker(_bcStage, ACCOUNT_ID, BRIDGE_MODE, DEBUG_MODE);
     _bcVideo = player.getModule(APIModules.VIDEO_PLAYER) as VideoPlayerModule;
      if (_bcVideo != null) {
      _bcVideo.addEventListener(MediaEvent.BEGIN, onMediaBegin);
      _bcVideo.addEventListener(MediaEvent.COMPLETE, onMediaComplete);
      // NOTE: you may choose to add more Brightcove player event listeners here
      }
     // if you want to track BC menu events, get a reference to the Menu module here
     //_bcMenu = player.getModule(APIModules.MENU) as MenuModule;
     fireEvent(EVENT_PLAYER_LOAD);
      }
     /**
      * Handler for when the player has access to the stage.
      *
      * @param event Event dispatched by ExperienceModule.
      */
      private function onAddedToStage(event:ExperienceEvent):void {
      _bcExperience.removeEventListener(ExperienceEvent.ADDED_TO_STAGE, onAddedToStage);
      registerEvents();
      }
     /**
      * Handler for when a new piece of media begins.
      *
      * @param event Event dispatched by VideoPlayerModule.
      */
      private function onMediaBegin(event:MediaEvent):void {
      fireEvent(EVENT_VIDEO_START);
      }
     /**
      * Handler for when a piece of media completes for the first time.
      *
      * @param event Event dispatched by VideoPlayerModule.
      */
      private function onMediaComplete(event:MediaEvent):void {
      fireEvent(EVENT_VIDEO_COMPLETED);
      }
     /*
      * The player is ready for interaction. Checks for access to stage.
      */
      override protected function initialize():void {
      _bcExperience = player.getModule(APIModules.EXPERIENCE) as ExperienceModule;
      _bcStage = _bcExperience.getStage();
      if (_bcStage == null) {
      _bcExperience.addEventListener(ExperienceEvent.ADDED_TO_STAGE, onAddedToStage);
      } else {
      registerEvents();
      }
    }
  
  }
}
Tags
sample, utility SWF