Skip to content

Instantly share code, notes, and snippets.

@robrobbins
Created March 4, 2014 23:18
Show Gist options
  • Save robrobbins/9357818 to your computer and use it in GitHub Desktop.
Save robrobbins/9357818 to your computer and use it in GitHub Desktop.
A callable version of the Google Adwords conversion script that doesn't use document.write
window.google_conversion_function = function () {
var g = this;
var h = parseFloat("0.06"),
k = isNaN(h) || 1 < h || 0 > h ? 0 : h;
var l = function (a, c) {
for (var d in a) Object.prototype.hasOwnProperty.call(a, d) && c.call(null, a[d], d, a)
};
var m, n, p, q, r = function () {
return g.navigator ? g.navigator.userAgent : null
};
q = p = n = m = !1;
var s;
if (s = r()) {
var t = g.navigator;
m = 0 == s.lastIndexOf("Opera", 0);
n = !m && (-1 != s.indexOf("MSIE") || -1 != s.indexOf("Trident"));
p = !m && -1 != s.indexOf("WebKit");
q = !m && !p && !n && "Gecko" == t.product
}
var u = n,
v = q,
w = p;
var x;
if (m && g.opera) {
var y = g.opera.version;
"function" == typeof y && y()
} else v ? x = /rv\:([^\);]+)(\)|;)/ : u ? x = /\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/ : w && (x = /WebKit\/(\S+)/), x && x.exec(r());
var z = function (a) {
this.b = [];
this.a = {};
for (var c = 0, d = arguments.length; c < d; ++c) this.a[arguments[c]] = ""
}, C = function () {
var a = A,
c = "317150500 317150501 317150502 317150503 317150504 317150505".split(" ");
if (a.a.hasOwnProperty(1) && "" == a.a[1]) {
r: {
if (!(1E-4 > Math.random())) {
var d = Math.random();
if (d < k) {
try {
var b = new Uint16Array(1);
window.crypto.getRandomValues(b);
d = b[0] / 65536
} catch (e) {
d = Math.random()
}
c = c[Math.floor(d * c.length)];
break r
}
}
c = null
}
c && "" != c && a.a.hasOwnProperty(1) && (a.a[1] = c)
}
};
z.prototype.c = function (a) {
return this.a.hasOwnProperty(a) ? this.a[a] : ""
};
z.prototype.geil = z.prototype.c;
var D = function () {
var a = A,
c = [];
l(a.a, function (a) {
"" != a && c.push(a)
});
return 0 < a.b.length && 0 < c.length ? a.b.join(",") + "," + c.join(",") : a.b.join(",") + c.join(",")
};
var A, E = "google_conversion_id google_conversion_format google_conversion_type google_conversion_order_id google_conversion_language google_conversion_value google_conversion_currency_code google_conversion_domain google_conversion_label google_conversion_color google_disable_viewthrough google_remarketing_only google_remarketing_for_search google_conversion_items google_custom_params google_conversion_date google_conversion_time google_conversion_js_version onload_callback opt_image_generator google_is_call google_conversion_page_url".split(" ");
function F(a) {
return null != a ? escape(a.toString()) : ""
}
function G(a) {
return null != a ? a.toString().substring(0, 512) : ""
}
function H(a, c) {
var d = F(c);
if ("" != d) {
var b = F(a);
if ("" != b) return "&".concat(b, "=", d)
}
return ""
}
function I(a) {
var c = typeof a;
return null == a || "object" == c || "function" == c ? null : String(a).replace(/,/g, "\\,").replace(/;/g, "\\;").replace(/=/g, "\\=")
}
function J(a) {
var c;
if ((a = a.google_custom_params) && "object" == typeof a && "function" != typeof a.join) {
var d = [];
for (c in a)
if (Object.prototype.hasOwnProperty.call(a, c)) {
var b = a[c];
if (b && "function" == typeof b.join) {
for (var e = [], f = 0; f < b.length; ++f) {
var B = I(b[f]);
null != B && e.push(B)
}
b = 0 == e.length ? null : e.join(",")
} else b = I(b);
(e = I(c)) && null != b && d.push(e + "=" + b)
}
c = d.join(";")
} else c = "";
return "" == c ? "" : "&".concat("data=", encodeURIComponent(c))
}
function K(a) {
return "number" != typeof a && "string" != typeof a ? "" : F(a.toString())
}
function L(a) {
if (!a) return "";
a = a.google_conversion_items;
if (!a) return "";
for (var c = [], d = 0, b = a.length; d < b; d++) {
var e = a[d],
f = [];
e && (f.push(K(e.value)), f.push(K(e.quantity)), f.push(K(e.item_id)), f.push(K(e.adwords_grouping)), f.push(K(e.sku)), c.push("(" + f.join("*") + ")"))
}
return 0 < c.length ? "&item=" + c.join("") : ""
}
function M(a, c, d) {
var b = [];
if (a) {
var e = a.screen;
e && (b.push(H("u_h", e.height)), b.push(H("u_w", e.width)), b.push(H("u_ah", e.availHeight)), b.push(H("u_aw", e.availWidth)), b.push(H("u_cd", e.colorDepth)));
a.history && b.push(H("u_his", a.history.length))
}
d && "function" == typeof d.getTimezoneOffset && b.push(H("u_tz", -d.getTimezoneOffset()));
c && ("function" == typeof c.javaEnabled && b.push(H("u_java", c.javaEnabled())), c.plugins && b.push(H("u_nplug", c.plugins.length)), c.mimeTypes && b.push(H("u_nmime", c.mimeTypes.length)));
return b.join("")
}
function N(a, c, d) {
var b = "";
if (c) {
var e;
if (a.top == a) e = 0;
else {
var f = a.location.ancestorOrigins;
if (f) e = f[f.length - 1] == a.location.origin ? 1 : 2;
else {
f = a.top;
try {
e = !! f.location.href || "" === f.location.href
} catch (B) {
e = !1
}
e = e ? 1 : 2
}
}
f = "";
f = d ? d : 1 == e ? a.top.location.href : a.location.href;
b += H("frm", e);
b += H("url", G(f));
b += H("ref", G(c.referrer))
}
return b
}
function O(a) {
return a && a.location && a.location.protocol && "https:" == a.location.protocol.toString().toLowerCase() ? "https:" : "http:"
}
function P(a) {
return a.google_remarketing_only ? "googleads.g.doubleclick.net" : a.google_conversion_domain || "www.googleadservices.com"
}
function Q(a, c, d, b) {
var e = "/?";
"landing" == b.google_conversion_type && (e = "/extclk?");
var e = O(a) + "//" + P(b) + "/pagead/" + [b.google_remarketing_only ? "viewthroughconversion/" : "conversion/", F(b.google_conversion_id), e, "random=", F(b.google_conversion_time)].join(""),
f;
r: {
f = b.google_conversion_language;
if (null != f) {
f = f.toString();
if (2 == f.length) {
f = H("hl", f);
break r
}
if (5 == f.length) {
f = H("hl", f.substring(0, 2)) + H("gl", f.substring(3, 5));
break r
}
}
f = ""
}
a = [H("cv", b.google_conversion_js_version), H("fst", b.google_conversion_first_time),
H("num", b.google_conversion_snippets), H("fmt", b.google_conversion_format), H("value", b.google_conversion_value), H("currency_code", b.google_conversion_currency_code), H("label", b.google_conversion_label), H("oid", b.google_conversion_order_id), H("bg", b.google_conversion_color), f, H("guid", "ON"), H("disvt", b.google_disable_viewthrough), H("is_call", b.google_is_call), H("eid", D()), L(b), M(a, c, b.google_conversion_date), J(b), N(a, d, b.google_conversion_page_url), b.google_remarketing_for_search && !b.google_conversion_domain ?
"&srr=n" : ""
].join("");
return e + a
}
function R() {
var a = S,
c = document,
d = T,
b;
b = O(a) + "//www.google.com/ads/user-lists/" + [F(d.google_conversion_id), "/?random=", Math.floor(1E9 * Math.random())].join("");
return b += [H("label", d.google_conversion_label), H("fmt", "3"), N(a, c, d.google_conversion_page_url)].join("")
}
function U(a) {
return {
ar: 1,
bg: 1,
cs: 1,
da: 1,
de: 1,
el: 1,
en_AU: 1,
en_US: 1,
en_GB: 1,
es: 1,
et: 1,
fi: 1,
fr: 1,
hi: 1,
hr: 1,
hu: 1,
id: 1,
is: 1,
it: 1,
iw: 1,
ja: 1,
ko: 1,
lt: 1,
nl: 1,
no: 1,
pl: 1,
pt_BR: 1,
pt_PT: 1,
ro: 1,
ru: 1,
sk: 1,
sl: 1,
sr: 1,
sv: 1,
th: 1,
tl: 1,
tr: 1,
vi: 1,
zh_CN: 1,
zh_TW: 1
}[a] ? a + ".html" : "en_US.html"
}
function V() {
var a = S,
c = navigator,
d = document,
b = S;
3 != b.google_conversion_format || b.google_remarketing_only || b.google_conversion_domain || A && C();
var e = A ? A.c(1) : "",
c = Q(a, c, d, b),
d = function (a, b, c) {
return '<img height="' + c + '" width="' + b + '" border="0" alt="" src="' + a + '" />'
};
return 0 == b.google_conversion_format && null == b.google_conversion_domain ? '<a href="' + (O(a) + "//services.google.com/sitestats/" + U(b.google_conversion_language) + "?cid=" + F(b.google_conversion_id)) + '" target="_blank">' + d(c, 135, 27) + "</a>" : 1 < b.google_conversion_snippets ||
3 == b.google_conversion_format ? "317150501" == e || "317150502" == e || "317150503" == e || "317150504" == e || "317150505" == e ? d(c, 1, 1) + ('<script src="' + c.replace(/(&|\?)fmt=3(&|$)/, "$1fmt=4&adtest=on$2") + '">\x3c/script>') : d(c, 1, 1) : '<iframe name="google_conversion_frame" title="Google conversion frame" width="' + (2 == b.google_conversion_format ? 200 : 300) + '" height="' + (2 == b.google_conversion_format ? 26 : 13) + '" src="' + c + '" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no">' +
d(c.replace(/\?random=/, "?frame=0&random="), 1, 1) + "</iframe>"
}
function aa() {
return new Image
}
function ba() {
var a = T,
c = R(),
d = aa;
"function" === typeof a.opt_image_generator && (d = a.opt_image_generator);
a = d();
c += H("async", "1");
a.src = c;
a.onload = function () {}
};
var S = window;
if (S)
if (null != /[\?&;]google_debug/.exec(document.URL)) {
var ca = S,
W = document.getElementsByTagName("head")[0];
W || (W = document.createElement("head"), document.getElementsByTagName("html")[0].insertBefore(W, document.getElementsByTagName("body")[0]));
var X = document.createElement("script");
X.src = O(window) + "//" + P(ca) + "/pagead/conversion_debug_overlay.js";
W.appendChild(X)
} else {
try {
var Y;
var Z = S;
var _el_ = document.createElement('div');
"landing" == Z.google_conversion_type || !Z.google_conversion_id || Z.google_remarketing_only && Z.google_disable_viewthrough ? Y = !1 : (Z.google_conversion_date = new Date, Z.google_conversion_time = Z.google_conversion_date.getTime(), Z.google_conversion_snippets = "number" == typeof Z.google_conversion_snippets && 0 < Z.google_conversion_snippets ? Z.google_conversion_snippets + 1 : 1, "number" != typeof Z.google_conversion_first_time && (Z.google_conversion_first_time = Z.google_conversion_time), Z.google_conversion_js_version = "7", 0 != Z.google_conversion_format && 1 != Z.google_conversion_format && 2 != Z.google_conversion_format && 3 != Z.google_conversion_format &&
(Z.google_conversion_format = 1), A = new z(1), Y = !0);
_el_.innerHTML = V();
// don't see how this will ever be truthy. that looks to be fine in our case
if (Y && (document.querySelector('body').appendChild(_el_), S.google_remarketing_for_search && !S.google_conversion_domain)) {
var T = S;
ba()
}
} catch (da) {}
for (var ea = S, $ = 0; $ < E.length; $++) ea[E[$]] = null
};
};
@asdf1011
Copy link

Another option is to use the async version of the remarketing tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment