How do I set up an AdWords tracking pixel to work in AngularJS?
A typical tracking code is as follows:
<script type="text/javascript"> var google_conversion_id = 123456789; var google_conversion_language = "en"; var google_conversion_format = "2"; var google_conversion_color = "ffffff"; var google_conversion_label = "AAAAAAAAAAAAAAAAAAA"; var google_conversion_value = 0; </script> <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"> </script>
(I omitted the standard <noscript> reserve, as it is clearly not relevant to the AngularJS application context.)
Tracking code works by setting up a bunch of variables in the global namespace and then fetching an external script for each page load. In the context of Angular, this does not work because the HTML source is not retrieved from the server each time the page loads.
My initial (and possibly non-functional) attempt to adapt it to Angular looks like this (in Coffeescript):
SpiffyApp.run ($rootScope, $location, $window, session, flash) -> # Other initialization stuff $rootScope.$on '$routeChangeSuccess', (event, data) -> # Other route-change callback stuff $window.google_conversion_id = 123456789 $window.google_conversion_language = "en" $window.google_conversion_format = "2" $window.google_conversion_color = "ffffff" $window.google_conversion_label = "AAAAAAAAAAAAAAAAAAA" $window.google_conversion_value = 0 jQuery.ajax type: "GET", url: "//www.googleadservices.com/pagead/conversion.js", dataType: "script", cache: true
This does not work. At the very least, marketing consultants claim such. I admit there is a pretty decent PEBKAC chance here, so my questions are:
- Should the above work?
- If not, what will work?
Thanks in advance!
PS: I inherited this application from another developer, and I am not very well versed in the platform. Feel free to mention (in the comments) any terribly bad code / methods above. Thanks!
angularjs coffeescript google-adwords
Daniel Wright
source share