The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

Dynamic conversion values for Google Adwords

joelataylorjoelataylor Member
in Help edited September 2012
I've had a client who wanted me to setup their Google Adword conversion tracking with a calculated profit value. So, I created a script that received params from a json call (on the receipt page) and it returned a json object that had the profit value.

I then loaded that value into the default google tracking js variable, and tried to call the conversion js script via jQuery's getScript method.

However, the tracking code never got passed. After some help from @brett - we found an article that pointed out the problem and a solution: http://articles.adamwrobel.com/2010/12/23/trigger-adwords-conversion-on-javascript-event

Basically, it's a matter of rewriting the document.write function, like this:
.complete(function(){
			// Now load the Google Conversion script
			document.write = function(text) {
			  $('body').append(text);
			};
			$.getScript('http://www.googleadservices.com/pagead/conversion.js');
		});

So basically, in my receipt template, I have this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setAllowAnchor', true]);
_gaq.push(['_trackPageview', '/receipt']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

^^receipt_only_begin^^
^^analytics_google_ga_async^^

<script type="text/javascript">
	var JSON = JSON || {};
	JSON.stringify = JSON.stringify || function (obj) {
		var t = typeof (obj);
		if (t != "object" || obj === null) {
			// simple data type
			if (t == "string") obj = '"'+obj+'"';
			return String(obj);
		} else {
			// recurse array or object  
			var n, v, json = [], arr = (obj && obj.constructor == Array);  
			for (n in obj) {  
				v = obj[n]; t = typeof(v);  
				if (t == "string") v = '"'+v+'"';  
				else if (t == "object" && v !== null) v = JSON.stringify(v);  
				json.push((arr ? "" : '"' + n + '":') + String(v));  
			}  
			return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");  
		}
	};

	var sp = 0;
	var google_conversion_id = XXXXXXXXXXX;
	var google_conversion_language = "en_US";
	var google_conversion_format = "3";
	var google_conversion_color = "ffffff";
	var google_conversion_label = "XXXXXXXXXXXXX";
	var google_conversion_value = null;
	
	var prods = JSON.stringify(fc_json.products);
	
	$.getJSON('https://yourdomain.com/ajax-calc.php?products='+prods+'&callback=?',function(data){
		if (data.error_code==undefined){
			sp = data.sp;
		} else {
			error = data.error_msg;
		}					
	})
	.success(function(data){
		google_conversion_value = data.sp;
	})
	.complete(function(){
		document.write = function(text) {
		  $('body').append(text);
		};
		$.getScript('http://www.googleadservices.com/pagead/conversion.js');
	});
</script>
^^receipt_only_end^^

Hope that's helpful for someone else. ;)
Comments
  • brettbrett FoxyCart Team
    For search engine fodder: This is about dynamically loading conversion.js after setting some of the required values. Using a document.write or $.getScript approach doesn't work, for reasons described in the above linked post.
Sign In or Register to comment.