AS3 Javascript Call Sequencer + Omniture + Google Analytics
Here’s a little utility that I did for multiple Omniture calls to a javascript in the html page. It will also accept another call, such as an iFrame refresh. It delays each call a tiny bit to make sure that events are fired off. Also has a runtime environment detection so your browser window doesnt keep popping up during publishing. Source file below as well.
Usage:
// Genric Javascript
function clickHandler(event:MouseEvent):void {
_oSendJavascript.addEvent(
“AutoReloadIFrame();”
);
}
//or…
// Omniture call
function clickHandler(event:MouseEvent):void {
_oSendJavascript.addEvent(
“var x = new Object(); x.pageName = ‘Primary Section - Secondary Section’;trackPageView(x);”
);
}
//or…
// Google Analytics call
function clickHandler(event:MouseEvent):void {
_oSendJavascript.addEvent(
“_uacct = ‘YOURGOOGLEACCOUNTNUMBER-000000-1′; urchinTracker();”
);
}
// And of course you can stack calls
_oSendJavascript.addEvent(“AutoReloadIFrame();”);
_oSendJavascript.addEvent(“function1();”);
_oSendJavascript.addEvent(“function2();”);
The class:
/**
* @author Donovan Adams
* @version September 28, 2007
*
* @description SendJavascript Tracking Utility. Queues up SendJavascript events that fire off incrementally.
*
* @usage Example:
<div class="codesnip-container" >// This should only be called once! THe SendJavascript class is a Singleton pattern
_oSendJavascript = SendJavascript.getInstance();
// Called whenever an even needs to be triggered.
function clickHandler(event:MouseEvent):void{
_oSendJavascript.addEvent("AutoReloadIFrame();");
}</div>
*/
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.system.Capabilities;
public class SendJavascript {
private static const DELAY:Number = .1; // Time in seconds
private static var _oSendJavascript : SendJavascript;
private static var _queueArray:Array;
private static var _queueTimer:Timer;
private static var _count:int;
public static function getInstance() : SendJavascript{
if (_oSendJavascript == null) _oSendJavascript = new SendJavascript();
return _oSendJavascript;
}
public function SendJavascript():void {
trace(‘\n\n\n\n======== SendJavascript Initializing ‘+(new Date()).toString()+‘=========\n\n‘);
_queueArray = [];
_count = 0;
_queueTimer = new Timer(DELAY*1000, 0);
_queueTimer.addEventListener(TimerEvent.TIMER, sendEvent);
_queueTimer.addEventListener(TimerEvent.TIMER_COMPLETE, queueComplete);
}
public function addEvent(p:String):void{
trace(“\t>> SendJavascript :: addEvent() - _count: “+_count);
_queueArray.push(“javascript: “ + p);
_queueTimer.repeatCount++;
if(!_queueTimer.running) sendQueue();
trace(_count);
_count++;
}
private function sendQueue():void{
trace(“\t>> SendJavascript :: sendQueue()”);
_queueTimer.reset();
_queueTimer.start();
}
private function sendEvent(event:TimerEvent):void{
trace(“\t>> SendJavascript :: sendEvent() - currentCount: “+_queueTimer.currentCount, _queueTimer.repeatCount);
/* you can add any other variables you want to track above x.sprop2, x.channel, etc; */
var playerType:String = Capabilities.playerType;
if (playerType == “External” || playerType == “StandAlone”) {
trace(“\t\tnavigateToURL(”+_queueArray[_queueTimer.currentCount - 1]+“),’_self’);”);
} else {
navigateToURL(new URLRequest(_queueArray[_queueTimer.currentCount - 1]),‘_self’);
}
}
private function queueComplete(event:TimerEvent):void {
trace(“\t>> SendJavascript - queueComplete() - type: “+event.type);
flush();
_queueTimer.reset();
}
private function flush():void{
trace(“\t>> SendJavascript :: flush()”);
_queueArray = [];
_count = _queueTimer.repeatCount = 0;
}
}
}
Javascript Call Utility Source
Friday, September 28th, 2007
