AS3 clicktag

A common problem with flash creatives developed in ActionScript 3 is getting the clicktag right. After extensive research and rigorous testing we've come up with the following ActionScript code:
function handleClick(mouseEvent:MouseEvent):void {
    var interactiveObject:InteractiveObject = mouseEvent.target as InteractiveObject;
    var li:LoaderInfo = LoaderInfo(interactiveObject.root.loaderInfo);
    var url:String = li.parameters.clickTag;
    if (url) {
        if (ExternalInterface.available) {
            var userAgent:String = ExternalInterface.call('function(){ return navigator.userAgent; }');

            if (userAgent.indexOf("MSIE") >= 0) {
                ExternalInterface.call('window.open', url, '_blank');
            } else {
                navigateToURL(new URLRequest(url), '_blank');
            }
        } else {
            navigateToURL(new URLRequest(url), '_blank');
        }
    }
}
myButton_btn.addEventListener(MouseEvent.MOUSE_UP,handleClick);
Please change the clickTag variable name (in bold) to the formatting required by your ad server (clicktag, clickTag or clickTAG).


Requirements

For this clicktag to work in all browsers the following two requirements need to be met:

  • The html object tag needs an id or Internet Explorer will generate a javascript error of "null is null or not an object".
  • AllowScriptAccess needs to be true either by setting it to "always" or by setting it to "sameDomain" and loading the flash from the same domain as the hosting page.


HTML to load AS3 flash creative

We suggest loading your flash with something similar to the following HTML (important parts in bold):

<object id="flash_file_1" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="980" height="120">
  <param name="movie" value="yourfile.swf">
  <param name="quality" value="autohigh">
  <param name="wmode" value="opaque">
  <param name="allowScriptAccess" value="always">
  <param name="FlashVars" value="clickTag=http%3A%2F%2Fexample.com">
  <embed src="yourfile.swf?clickTag=http%3A%2F%2Fexample.com" quality="autohigh" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="980" height="120"></embed>
</object>


More technical details

  • navigateToURL() will work in IE as long as wmode is set to window. With opaque or transparent wmode the navigateToURL() function will result in the IE popup blocker blocking your click.
  • The default AllowScriptAccess setting is "sameDomain"
  • ExternalInterface will only work if AllowScriptAccess is true
  • The ExternalInterface call to window.open() is unfortunately slightly slower than calling navigateToURL()

1 comment:

  1. Just curious - is there a reason for omitting the classic substring check on the clickTag to make sure that the URL is http and not something potentially naughty?

    ReplyDelete