09-30-2011 03:08 PM
Posting this here in the hope that it may help another previously frustrated developer.
For the last few months we've tried to get our comScore plugin working correctly with our new BrightCove players. A little history: Long story short - as BrightCove updated, a previous comScore plugin stopped working. We had problems trying to figure out how to track both ads and content in our players. So for awhile we just hardcoded content to comScore, even though an ad would play (this of course isn't ideal).
C5=02 is for Content C5=09 is for Advertising for ComScore
Anyways using both the GoogleAnalytics plugin features on BrightCove AND the advertising api module code found here and here. We were able to create a plugin that worked with our GoogleAnalytics account as well as track both ads AND content for comScore.
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"; // Main : Your Google Account ID is loaded from the URL parameter 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";
// Ad Event Names
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;
// our C variables from HTML params
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;
//comScore5 = this._bcStage.root.loaderInfo.parameters.c5; // Needs to be set form code below not params
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.ADD ED_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
Solved! Go to Solution.
09-30-2011 03:30 PM
Great work and thanks so much for posting this!
Brightcove Inc., a leading global provider of cloud content services, provides a family of products used to publish and distribute the world's professional digital media. The company's products include Brightcove Video Cloud, the market-leading online video platform, and Brightcove App Cloud, the pioneering content app platform. Together, more than 4,200 customers in 50 countries rely on Brightcove's cloud content services to build and operate exceptional media experiences across PCs, smartphones, tablets and connected TVs.
Brightcove Inc.
© 2012 Brightcove Inc.
