Implementing tracking pixels in AS3.0: Mediaplex

Embedding tracking pixels in a Flash app sounds like it should be simple. Just make a Request to the server from your .swf that calls up the 1×1 .gif, right? Somehow or other it ends up being a confusing process nonetheless.

In my humble experience, the oversight is usually on the side of the vendor whose technology a Developer is asked to implement. Simple requests for instructions on how to implement a 3rd party technology into an Actionscript 3.0 application are met with confused email responses by folks who don’t seem to understand that there’s more than one version of the Actionscript programming language and the occasional emailed PDF that describes things like “How to Add a clickTag” using screen shots from Flash 5 and Asctionscript 1.0. Since more often than not vendors can’t or won’t send clear instructions on how to implement their tags in the current version of Actionscript / Flash / Flex, what ends up happening is the Developer is asked to test and debug the vendor’s product for them, for free.

Going forward, it’d be nice if a company that sells technology which they say can be implemented in Flash could actually have someone in that company:

  1. create a modern Flash application
  2. implement the technology
  3. have the source files ready for clients to use for reference.

OK, enough venting.

Here’s one way to implement Mediaplex tracking pixels in AS3.0 (Flash / Flex)

Make sure you receive the Standard pixel path, i.e. one that starts with “<img” (not one that starts with “<iframe”). Here’s a basic test class that calls a Mediaplex tracking pixel:

package
{
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.URLLoader;
	import flash.net.URLRequestMethod;
	import flash.display.Sprite;

	[SWF (width="500", height="750", backgroundColor="#000000", frameRate="31")]
	public class Main extends Sprite
	{
		private var yourMediaPlexURL:Array = ["http://sd.mediaplex.com/somepath/"];

		public function Main():void
		{
			Security.loadPolicyFile("http://www.your.com/crossdomain.xml");
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		public function  onAddedToStage(e:Event):void
		{
			//call the tracking code
			trackMediaPlex(yourMediaPlexURL[0]);
		}

		public function trackMediaPlex(yourURL:String):void
		{
			var myRequest:URLRequest = new URLRequest(yourURL);
			myRequest.method = URLRequestMethod.POST;

			var myLoader:URLLoader = new URLLoader();
			try{
				myLoader.load(myRequest);
				trace("\t trackMediaPlex(): myRequest.url = " + myRequest.url);
			} catch(e:Error) {
				trace(e.getStackTrace());
			}
		}
	}
}

Before activating the pixel, it’s a good idea to quickly test it in Firefox using Firebug. Under the Net tab[1] in in Firebug you should see three server hits:

  1. your original request
  2. a redirect
  3. a request for the 1×1 pixel file itself.

Once you see these firing in Firebug, activate the pixel and you should be good to go. For more detailed testing use Charles (Mac) and Fiddler (PC).

A basic crossdomain.xml policy file should allow all the potential Mediaplex urls that might be used, such as this one: http://somedomain.mediaplex.com. Be careful about how you specify urls in crossdomain.xml, the minuscule difference between “http://somedomain.mediaplex.com&#8221; and “http://somedomain.mediaplex.com/&#8221; can break your code.

Notes:
1. Make sure you Enable the Net tab first

Fun with Google Analytics in the Flash IDE with AS3

ExternalInterface.call()
There’s always External Interface method. Some good tutorials on this out there too.

So far I found 1 Flashcoders post about a bug in IE7 that doesn’t allow the use of this technique.

nagivateToURL()
People seem to be complaining about unwanted popup windows with this method. It seems to work fine for me in the browser, both locally and off a server. Popup windows only show up with Test Movie, which, while annoying, will not be an issue with clients.

Google’s AS3 component / SWC library
Google has an Analytics component and an Analytics Library component that doesn’t require any javascript on your page and talks to their analytics system directly. Only problem seems to be it adds between 60KB and 40KB to your file size. The cool part – you don’t have to rely on Javascript at all.

SWFaddress for Google Analytics tracking
There’s a way to use SWFaddress 2.0 together with ga.js to track page views in Flash sites.

sendToURL()
There’re a few posts from 2007 that utilize the global function sendToURL().

/* ... */
var trgtURL:URLRequest = new URLRequest("javascript:pageTracker._trackPageview('mypath/mypage_link');");
sendToURL(trgtURL);
/* ... */

This technique wouldn’t work & gave me a security error every time, both during Test Movie and from a live server. I suspect the Flash player security changes between 9,0115 and 10 banned this method. Here’s the error: “Security Error: Error #2169: The method sendToURL may not be used for browser scripting. The URL javascript:pageTracker._trackPageview…”

getURL() ?
Oh, I hope not…

Something like this should work OK, given you import all the proper classes & Google’s javascript code is on the html page that displays your .SWF:

//...       
       trackTheGoogley("mysite.com/mycategory/mytrackerTextHere");

		public function trackTheGoogley(trackString:String):void
		{			
			if(ExternalInterface.available){
				ExternalInterface.call("pageTracker._trackPageview",trackString);
			} else {
				navigateToURL(new URLRequest("javascript:pageTracker._trackPageview('"+trackString+"')"), "_self");
			}
		}
//...