Last active
October 25, 2018 20:05
-
-
Save vhsu/39d491a66afca463ae2e to your computer and use it in GitHub Desktop.
Google Adwords Asynchronous Remarketing Javascript Module - Lazy Loading Adwords Conversion Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This module can be used to trigger the Google Adwords remarketing or conversion tag asynchronously only when you need it (lazy loading) without loading unused ressources | |
// Mostly useful when the Google Remarketing tag or Conversion tag is triggered only on specific conditions. | |
// Author : Vincent Hsu -> twitter.com/suisseo -> http://www.suisseo.ch | |
// Language : Javascript | |
// Usage : googremarketing.loadTag(conversionid, conversionlabel) | |
// Start Google Remarketing Module | |
var googremarketing = (function() { | |
var asyncload = 0; | |
// Load Async Google Adwords remarketing code | |
function Gremloader() { | |
if (asyncload == 0) { | |
var g = document.createElement('script'); | |
var s = document.getElementsByTagName('script')[0]; | |
g.src = 'https://www.googleadservices.com/pagead/conversion_async.js'; | |
s.parentNode.insertBefore(g, s); | |
asyncload = 1; | |
} | |
} | |
// Wait... | |
function whenAvailable(name, callback) { | |
var interval = 20; // ms | |
window.setTimeout(function recName() { | |
window[name] && callback(window[name]) || window.setTimeout(recName, interval) | |
}, interval); | |
} | |
//Set Google Remarketing Tag | |
function setTag(convid, convlabel) { | |
window.google_trackConversion({ | |
google_conversion_id: convid, | |
google_conversion_label: convlabel, | |
google_remarketing_only: true | |
}); | |
} | |
return { | |
loadTag: function(conversionid, conversionlabel) { | |
Gremloader(); | |
whenAvailable("google_trackConversion", function(t) { | |
setTag(conversionid, conversionlabel); | |
}); | |
} | |
} | |
}()); | |
// End of Google Remarketing module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment