Return to Snippet

Revision: 51670
at October 1, 2011 05:48 by leongaban


Initial Code
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.events.AdEvent;
	import com.brightcove.api.modules.ExperienceModule;
	import com.brightcove.api.modules.MenuModule;
	import com.brightcove.api.modules.VideoPlayerModule;
	import com.brightcove.api.modules.AdvertisingModule;

	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;

	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.Security;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.external.ExternalInterface;
	import flash.system.Security;
	import flash.utils.*;


	public class GoogleAnalytics extends CustomModule
	{

		// ----------------------------------------------------------------------------
		// Constants
		private const ACCOUNT_ID  : String = "UA-1234567-10";  // GoogleAnalytics ID
		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 const AD_START              : String = "adStart";

		// ----------------------------------------------------------------------------

		private var _bcExperience:ExperienceModule;
		private var _bcAds:AdvertisingModule;
		private var _bcVideo:VideoPlayerModule;
		private var _bcStage:Stage;
		private var _tracker:AnalyticsTracker;

		// from HTML
		private var comScore1:String = "";
		private var comScore2:String = "";
		private var comScore3:String = "";
		private var comScore4:String = "";

		/**
		* 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
		{
			comScore1 = this._bcStage.root.loaderInfo.parameters.c1;
			comScore2 = this._bcStage.root.loaderInfo.parameters.c2;
			comScore3 = this._bcStage.root.loaderInfo.parameters.c3;
			comScore4 = this._bcStage.root.loaderInfo.parameters.c4;

			var experienceId : Number = _bcExperience.getExperienceID();
			var playerName   : String = _bcExperience.getPlayerName();
			var action       : String = "";

			switch (eventName)
			{

				// Player Loads
				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;

				// Video Plays
				case EVENT_VIDEO_START :
					comScoreBeacon(comScore1, comScore2, comScore3, comScore4, "02", "ShowLevelID", "SegmentID");
					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;
			}

			// Google Analytics
			_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);
			_bcAds   = player.getModule(APIModules.ADVERTISING) as AdvertisingModule;
			_bcVideo = player.getModule(APIModules.VIDEO_PLAYER) as VideoPlayerModule;

			if (_bcVideo != null)
			{

				// Media EventListeners
				_bcVideo.addEventListener(MediaEvent.BEGIN, onMediaBegin);
				_bcVideo.addEventListener(MediaEvent.COMPLETE, onMediaComplete);
				_bcAds.addEventListener(MediaEvent.COMPLETE, onMediaComplete);

				// Advertising EventListeners
				/*_bcAds.addEventListener(AdEvent.AD_COMPLETE, onAdComplete);
				_bcAds.addEventListener(AdEvent.AD_PAUSE, onAdPause);
				_bcAds.addEventListener(AdEvent.AD_PROGRESS, onAdProgress);
				_bcAds.addEventListener(AdEvent.AD_RESUME, onAdResume);*/
				_bcAds.addEventListener(AdEvent.AD_START, onAdStart);
			}

			// 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);
		}

		// Advertisment starts
		private function onAdStart(event:Event):void
		{
			comScoreBeacon(comScore1, comScore2, comScore3, comScore4, "09", "ShowLevelID", "SegmentID");
			_bcAds.removeEventListener(AdEvent.AD_START, onAdStart);
		}

		/**
		* 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();
			}
		}

		// comScore function
		private function comScoreBeacon(c1:String, c2:String, c3:String, c4:String, c5:String, c6:String, c10:String):String
		{
			var page:String = "",referrer:String = "",title:String = "";

			try
			{
				page = ExternalInterface.call("function() { return document.location.href; }").toString();
				referrer = ExternalInterface.call("function() { return document.referrer; }").toString();
				title = ExternalInterface.call("function() { return document.title; }").toString();
				if (typeof(page) == "undefined" || page == "null")
				{
					page = loaderInfo.url;
				}
				if (typeof(referrer) == "undefined" || referrer == "null")
				{
					referrer = "";
				}
				if (typeof(title) == "undefined" || title == "null")
				{
					title = "";
				}
				if (page != null && page.length > 512)
				{
					page = page.substr(0,512);
				}
				if (referrer.length > 512)
				{
					referrer = referrer.substr(0,512);
				}
			}
			catch (e:Error)
			{
				page = loaderInfo.url;
				trace(e);
			}

			var url:String = (new Array(
			page.indexOf("https:") == 0 ? "https://sb" : "http://b",
			".scorecardresearch.com/p",
			"?c1=", c1,
			"&c2=", escape(c2),
			"&c3=", escape(c3),
			"&c4=", escape(c4),
			"&c5=", escape(c5),
			"&c6=", escape(c6),
			"&c7=", escape(page),
			"&c8=", escape(title),
			"&c9=", escape(referrer),
			"&c10=", escape(c10),
			"&rn=", Math.random(),
			"&cv=2.0"
			)).join("");

			if (url.length > 2080)
			{
				url = url.substr(0,2080);
			}

			var loader:URLLoader = new URLLoader();
			loader.load(new URLRequest(url));

			return url;
		}

	}//Class

}//package

Initial URL


Initial Description
ComScore previously had a BrightCove plugin which now no longer works with the updated BrightCove players. Here I used the GoogleAnalytics plugin code BrightCove provided and piggy-backed the ComScore code onto it. Using the Advertising Module API in order to allow ComScore to track both ads and video content.

Initial Title
BrightCove GoogleAnalytics + ComScore Plugin

Initial Tags
google

Initial Language
ActionScript 3