BrightCove GoogleAnalytics + ComScore Plugin


/ Published in: ActionScript 3
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. package
  2. {
  3.  
  4. import com.brightcove.api.APIModules;
  5. import com.brightcove.api.CustomModule;
  6. import com.brightcove.api.events.ExperienceEvent;
  7. import com.brightcove.api.events.MediaEvent;
  8. import com.brightcove.api.events.AdEvent;
  9. import com.brightcove.api.modules.ExperienceModule;
  10. import com.brightcove.api.modules.MenuModule;
  11. import com.brightcove.api.modules.VideoPlayerModule;
  12. import com.brightcove.api.modules.AdvertisingModule;
  13.  
  14. import com.google.analytics.AnalyticsTracker;
  15. import com.google.analytics.GATracker;
  16.  
  17. import flash.display.Loader;
  18. import flash.display.LoaderInfo;
  19. import flash.display.Sprite;
  20. import flash.display.Stage;
  21. import flash.events.Event;
  22. import flash.events.MouseEvent;
  23. import flash.system.Security;
  24. import flash.net.URLLoader;
  25. import flash.net.URLRequest;
  26. import flash.net.URLVariables;
  27. import flash.external.ExternalInterface;
  28. import flash.system.Security;
  29. import flash.utils.*;
  30.  
  31.  
  32. public class GoogleAnalytics extends CustomModule
  33. {
  34.  
  35. // ----------------------------------------------------------------------------
  36. // Constants
  37. private const ACCOUNT_ID : String = "UA-1234567-10"; // GoogleAnalytics ID
  38. private const BRIDGE_MODE : String = "AS3";
  39. private const DEBUG_MODE : Boolean = false;
  40.  
  41. // ----------------------------------------------------------------------------
  42. // Event Names
  43. private const EVENT_PLAYER_LOAD : String = "player_load";
  44. private const EVENT_VIDEO_START : String = "video_start";
  45. private const EVENT_VIDEO_COMPLETED : String = "video_complete";
  46. private const AD_START : String = "adStart";
  47.  
  48. // ----------------------------------------------------------------------------
  49.  
  50. private var _bcExperience:ExperienceModule;
  51. private var _bcAds:AdvertisingModule;
  52. private var _bcVideo:VideoPlayerModule;
  53. private var _bcStage:Stage;
  54. private var _tracker:AnalyticsTracker;
  55.  
  56. // from HTML
  57. private var comScore1:String = "";
  58. private var comScore2:String = "";
  59. private var comScore3:String = "";
  60. private var comScore4:String = "";
  61.  
  62. /**
  63. * Constructor.
  64. */
  65. public function GoogleAnalytics()
  66. {
  67. Security.allowDomain("*");
  68. }
  69.  
  70. // ----------------------------------------------------------------------------
  71. /**
  72. * Fire an event. This method has all the logic with respect to
  73. * how exactly to fire the event, as a page load or as a link
  74. * click.
  75. */
  76. private function fireEvent(eventName:String):void
  77. {
  78. comScore1 = this._bcStage.root.loaderInfo.parameters.c1;
  79. comScore2 = this._bcStage.root.loaderInfo.parameters.c2;
  80. comScore3 = this._bcStage.root.loaderInfo.parameters.c3;
  81. comScore4 = this._bcStage.root.loaderInfo.parameters.c4;
  82.  
  83. var experienceId : Number = _bcExperience.getExperienceID();
  84. var playerName : String = _bcExperience.getPlayerName();
  85. var action : String = "";
  86.  
  87. switch (eventName)
  88. {
  89.  
  90. // Player Loads
  91. case EVENT_PLAYER_LOAD :
  92. var experienceURL:String = _bcExperience.getExperienceURL();
  93. var referrerURL:String = _bcExperience.getReferrerURL();
  94. action = "/playerid=" + experienceId + "/playername=" + playerName + "/url=" + experienceURL + "/refurl=" + referrerURL + "/" + eventName;
  95. break;
  96.  
  97. // Video Plays
  98. case EVENT_VIDEO_START :
  99. comScoreBeacon(comScore1, comScore2, comScore3, comScore4, "02", "ShowLevelID", "SegmentID");
  100. break;
  101.  
  102. // add additional cases here if needed
  103. default :
  104. var video:Object = _bcVideo.getCurrentVideo();
  105. var playlistId:Number = video.lineupId;
  106. var videoId:Number = video.id;
  107. var videoName:String = video.displayName;
  108. action = "/playerid=" + experienceId + "/playername=" + playerName + "/playlistid=" + playlistId + "/videoid=" + videoId + "/videoname=" + videoName + "/" + eventName;
  109. }
  110.  
  111. // Google Analytics
  112. _tracker.trackPageview(action);
  113. }
  114.  
  115. /**
  116. * Register for all interesting events here.
  117. */
  118. private function registerEvents():void
  119. {
  120. _bcStage = _bcExperience.getStage();
  121.  
  122. // Create a Google tracker with reference to the Brightcove player stage and your Google Account ID
  123. _tracker = new GATracker(_bcStage,ACCOUNT_ID,BRIDGE_MODE,DEBUG_MODE);
  124. _bcAds = player.getModule(APIModules.ADVERTISING) as AdvertisingModule;
  125. _bcVideo = player.getModule(APIModules.VIDEO_PLAYER) as VideoPlayerModule;
  126.  
  127. if (_bcVideo != null)
  128. {
  129.  
  130. // Media EventListeners
  131. _bcVideo.addEventListener(MediaEvent.BEGIN, onMediaBegin);
  132. _bcVideo.addEventListener(MediaEvent.COMPLETE, onMediaComplete);
  133. _bcAds.addEventListener(MediaEvent.COMPLETE, onMediaComplete);
  134.  
  135. // Advertising EventListeners
  136. /*_bcAds.addEventListener(AdEvent.AD_COMPLETE, onAdComplete);
  137. _bcAds.addEventListener(AdEvent.AD_PAUSE, onAdPause);
  138. _bcAds.addEventListener(AdEvent.AD_PROGRESS, onAdProgress);
  139. _bcAds.addEventListener(AdEvent.AD_RESUME, onAdResume);*/
  140. _bcAds.addEventListener(AdEvent.AD_START, onAdStart);
  141. }
  142.  
  143. // if you want to track BC menu events, get a reference to the Menu module here
  144. //_bcMenu = player.getModule(APIModules.MENU) as MenuModule;
  145.  
  146. fireEvent(EVENT_PLAYER_LOAD);
  147. }
  148.  
  149. // Advertisment starts
  150. private function onAdStart(event:Event):void
  151. {
  152. comScoreBeacon(comScore1, comScore2, comScore3, comScore4, "09", "ShowLevelID", "SegmentID");
  153. _bcAds.removeEventListener(AdEvent.AD_START, onAdStart);
  154. }
  155.  
  156. /**
  157. * Handler for when the player has access to the stage.
  158. *
  159. * @param event Event dispatched by ExperienceModule.
  160. */
  161. private function onAddedToStage(event:ExperienceEvent):void
  162. {
  163. _bcExperience.removeEventListener(ExperienceEvent.ADDED_TO_STAGE, onAddedToStage);
  164. registerEvents();
  165. }
  166.  
  167. /**
  168. * Handler for when a new piece of media begins.
  169. *
  170. * @param event Event dispatched by VideoPlayerModule.
  171. */
  172. private function onMediaBegin(event:MediaEvent):void
  173. {
  174. fireEvent(EVENT_VIDEO_START);
  175. }
  176.  
  177. /**
  178. * Handler for when a piece of media completes for the first time.
  179. *
  180. * @param event Event dispatched by VideoPlayerModule.
  181. */
  182. private function onMediaComplete(event:MediaEvent):void
  183. {
  184. fireEvent(EVENT_VIDEO_COMPLETED);
  185. }
  186.  
  187. /*
  188. * The player is ready for interaction. Checks for access to stage.
  189. */
  190. override protected function initialize():void
  191. {
  192. _bcExperience = player.getModule(APIModules.EXPERIENCE) as ExperienceModule;
  193. _bcStage = _bcExperience.getStage();
  194.  
  195. if (_bcStage == null)
  196. {
  197. _bcExperience.addEventListener(ExperienceEvent.ADDED_TO_STAGE, onAddedToStage);
  198. }
  199. else
  200. {
  201. registerEvents();
  202. }
  203. }
  204.  
  205. // comScore function
  206. private function comScoreBeacon(c1:String, c2:String, c3:String, c4:String, c5:String, c6:String, c10:String):String
  207. {
  208. var page:String = "",referrer:String = "",title:String = "";
  209.  
  210. try
  211. {
  212. page = ExternalInterface.call("function() { return document.location.href; }").toString();
  213. referrer = ExternalInterface.call("function() { return document.referrer; }").toString();
  214. title = ExternalInterface.call("function() { return document.title; }").toString();
  215. if (typeof(page) == "undefined" || page == "null")
  216. {
  217. page = loaderInfo.url;
  218. }
  219. if (typeof(referrer) == "undefined" || referrer == "null")
  220. {
  221. referrer = "";
  222. }
  223. if (typeof(title) == "undefined" || title == "null")
  224. {
  225. title = "";
  226. }
  227. if (page != null && page.length > 512)
  228. {
  229. page = page.substr(0,512);
  230. }
  231. if (referrer.length > 512)
  232. {
  233. referrer = referrer.substr(0,512);
  234. }
  235. }
  236. catch (e:Error)
  237. {
  238. page = loaderInfo.url;
  239. trace(e);
  240. }
  241.  
  242. var url:String = (new Array(
  243. page.indexOf("https:") == 0 ? "https://sb" : "http://b",
  244. ".scorecardresearch.com/p",
  245. "?c1=", c1,
  246. "&c2=", escape(c2),
  247. "&c3=", escape(c3),
  248. "&c4=", escape(c4),
  249. "&c5=", escape(c5),
  250. "&c6=", escape(c6),
  251. "&c7=", escape(page),
  252. "&c8=", escape(title),
  253. "&c9=", escape(referrer),
  254. "&c10=", escape(c10),
  255. "&rn=", Math.random(),
  256. "&cv=2.0"
  257. )).join("");
  258.  
  259. if (url.length > 2080)
  260. {
  261. url = url.substr(0,2080);
  262. }
  263.  
  264. var loader:URLLoader = new URLLoader();
  265. loader.load(new URLRequest(url));
  266.  
  267. return url;
  268. }
  269.  
  270. }//Class
  271.  
  272. }//package

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.