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