Google Conversion tracking is immensely useful if your are using PPC (PayPerClick e.g. Adsense) to drive traffic to your rich media site, to help optimize (automatically, even!) the campaign as to what ads/keywords are using.
A few weeks ago, I was only used to working with google analytics. Which is conceptually similar, in the are both owned by google, both are used to measure business performance of your website or RIA, but it turns out completely different in implementation!
Google analytics is a general purpose hit tracking, and is useful especially inconjunction with A/B or multivariate testing of the site, without regards to how much money is being spent in the commerce side. Think of this as a traffic pattern of your site.
Google Conversion tracking is all about the $$$. Like keeping you from losing it! PPC campaigns can be quite expensive, $.25- $1.00 per click is pretty common in unoptimized campaigns. Given only like 1-3% of all the traffic will convert into a sale, making sure that you are making each precious marketing dollar go as far as possible.
Analytics is really easy to use with flash, there are libraries to call javascript to call google or through AS3 library call google directly. Conversion on the other hand, is hard to use with google. They give you a cut and paste code meant to be put into a html that loads once, However when you have a rich media site, AJAX, Flex there isn't really a page load during switching between segments.
There are 2 possible solutions so far.
1) DIV Injection
2) IFrames or Hidden Frame
SOLUTION #1: Div Injection
Injection is a superior choice as your flash can (security permitting) add all the javascript and related code it needs without the owner of the site having to do anything special.
This following self contained snippet could go in any AS3 file, to call the google Analytics pageTrack. It also niftily amends the document with the google javacript.
import flash.external.ExternalInterface; var myJavaScript:XML = <script> <![CDATA[ function(gaCode){ var gaScript = document.createElement("script"); gaScript.id="gaJS"; gaScript.type = "text/javascript"; gaScript.src ="http://www.google-analytics.com/ga.js"; var head = document.getElementsByTagName("head")[0]; head.appendChild(gaScript); // sometime after it's done loading the script above, call the page track // var myDiv = document.createElement("div"); myDiv.id ="track"; myDiv.style.width = "0px"; myDiv.style.height = "0px"; var ptScript = document.createElement("script"); ptScript.id="ptJS"; ptScript.type = "text/javascript"; ptScript.innerHTML ='try { var pageTracker = _gat._getTracker(\"'+gaCode+'\"); pageTracker._trackPageview();} catch(err) {}';//gaCode;//"alert('hello');"; //ptScript.appendChild(document.createTextNode("alert('hello');"); //didn't work for me. myDiv.appendChild(ptScript); document.body.appendChild(myDiv); } ]]> </script>; ExternalInterface.call(myJavaScript, conversion_code_txt.text); //e.g. "UA-12345678-9");//
Just copy and paste into a flash movie, publish and test on a webserver. You should be able to see with FireFox+FireBug the call to the ga.js library (if it's not already cached) and the second to the conversion script in the NET tab.
If you need more help on the approach, see this This 10 pager on actionscript.orrg
It is a GREAT resource on all the many things that can be done with the technique of javascript/vbscript injection.
SOLUTION #2: IFrames
Iframes are generally considered evil, but in this case they are necessary. Basically you have to call out from your actionscript code to a conversion javscript, which creates a iframe via javascript then loads in the html page
So YOUR ACTIONSCRIPT > ExternalInterface -> Javascript iframe loader-> iframe loads > google conversion code fires.
WARNING: we used this approach on one of our download pages, one where flash triggers a download of an installer simultaneously calling out to google Conversion. While it worked locally, when live on the website, only one call would work, and it seemed intermittent....
TESTING YOUR CONVERSIONS BEFORE GOING LIVE
WARNING: Google Conversions don't show up in reports right away taking 3-24 hours. So this can make testing slow.
Here's the protocol for testing
Go into your campaign manager, setup the conversion/action, then generate the conversion code you get from google into a file (e.g iconversion.html). MAKE SURE YOU PUT IT IN BETWEEN THE BODY TAGS. Put a javascript alert so you can see it working. e.g
Test that locally make sure you see the alert.
Upload that page to your server. e.g. www.troyworks.com/iconversion.html. Open up a browser and test that page in the browser, you should see the alert() you set up.
Setup a trial ad in your campaign that points to that, with some keyword unlikely to be clicked on (e..g I bid on my site troyworks.com). If your using a clients website for testing, make sure you have keywords corresponding to their site, or landing page text or google won't accept it (thinking it's spam)
Looking in the reports you should see 0 conversions. Give google a few minutes to promote the ad live.
Once it's finished (and assumingly approved) Open up a browser and search for the keyphrase you used in google.com, Your ad should show up on the side of the search results. Click on it, and you should be taken to the iconversion.html page, and see the alert pop up.
if you've gotten this far, then the next step is setting up the real landing page, and adding a javascript call to have that iconversion page loaded up into an hidden iframe. Let's call this index.html
Inside it you'll need a javascript area like
Then in your flash code you need something simple to call that javascript to call
NOTE: Sorry about the extra
tags, something wonky with my conversion routine.
I created a simple swf that just called that above function and used it in the index page. Testing locally as soon as the swf loaded I got the javascript alert.
Upload to the webserver, repeat the direct test, and then the test through the ad. You should see the alert and in the latter case 3-12 hours later the conversion should show up in your reports.
At this point you can turn on the automanage campaign feature...and google will try to get your bids low as possible and your positions as good as you've specified. Take down the scaffoling (e.g. alert tags) and use wherever you've decided on.
Good Luck!
{ 6 comments… read them below or add one }
Nice article Troy. It gave me some great insights on how to do this. One question though, why did you choose to use an iFrame instead of a div? Maybe something like this:
ExternalInterface.call("function() { " +
"myDiv = document.createElement("\"div\"); " +
"myDiv.style.width = 0 + \"px\"; " +
"myDiv.style.height = 0 + \"px\"; " +
"myDiv.innerHTML = \" " +
" alert('Insert conversion.js code here'); " +
"\"; " +
"document.body.appendChild(myDiv); " +
"}";
I stubbed this out for clarity. With this code, I wouldn’t need to rely on having a function inside of the HTML swf wrapper (which can accidentally get auto-regenerated by Flash, erasing your custom functions), and I’m also using a div which should be lighter than an iFrame.
Do you see any problems with doing this? Have you tried something similar but couldn’t get it to work?
Thanks in advance,
Nate
Hey All -
Let me tell you this is one tough piece of information to find!
I have no idea why.
Nate – Did your solution work for you? If I understand it, adding this to the AS would allow me to track a conversion without needing to open a new HTML page with it.
Boaz
I tried it, it appears to be working. I’ve updated the article.
Hey Nate,
Thanks for the inspiration! I was able to get a similar approach working and have updated the article with the code I used which is similar yet different than what you provided.
Thanks,
Troy.
Hi All,
I did get a heads up from a Flash developer telling me that if you put the entire javascript function within the Flash code, like in the example above, some internet security applications such as Norton will not allow that script to run.
If you have control over the HTML page that hold the flash element, and you can put the javascript function in the HTML page, you can call that function with the ExternalInterface in the Flash code.
For example, if I want to use Google Analytics Event Tracking I would write a javascript function liek this:
function trackFlashElement(category, action, label)
{
pageTracker._trackEvent(category, action, label);
}
and put it in the HTML page, and from the flash code I would use ExternalInterface to call that exact function with the relevant values.
wow great to know.
I suspect that that injection approach is used for many blackhat purposes thus the increased blocking by Norton etc.
I’ve noticed on some site it being used to popup windows that by pass normal popup blockers.