Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Facebook's new Audience ad tracking script
/*1464382852,,JIT Construction: v2361294,en_US*/
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*/
try {
(function(a, b, c, d) {
'use strict';
var e = "2.5.0",
f = 'https://www.facebook.com/tr/',
g = '/fbevents.',
h = {
IDENTITY: 'plugins.identity.js'
},
i = {},
j = [],
k = null,
l = null,
m = /^\d+$/,
n = {
allowDuplicatePageViews: false
},
o = function(ra) {
var sa = {
exports: ra
};
'use strict';
var ta = 'deep',
ua = 'shallow';
function va() {
this.list = [];
}
va.prototype = {
append: function(xa, ya) {
this._append(encodeURIComponent(xa), ya, ta);
},
_append: function(xa, ya, za) {
if (Object(ya) !== ya) {
this._appendPrimitive(xa, ya);
} else if (za === ta) {
this._appendObject(xa, ya);
} else this._appendPrimitive(xa, wa(ya));
},
_appendPrimitive: function(xa, ya) {
if (ya != null) this.list.push([xa, ya]);
},
_appendObject: function(xa, ya) {
for (var za in ya)
if (ya.hasOwnProperty(za)) {
var ab = xa + '[' + encodeURIComponent(za) + ']';
this._append(ab, ya[za], ua);
}
},
each: function(xa) {
var ya = this.list;
for (var za = 0, ab = ya.length; za < ab; za++) xa(ya[za][0], ya[za][1]);
},
toQueryString: function() {
var xa = [];
this.each(function(ya, za) {
xa.push(ya + '=' + encodeURIComponent(za));
});
return xa.join('&');
}
};
function wa(xa) {
if (typeof JSON === 'undefined' || JSON === null || !JSON.stringify) {
return Object.prototype.toString.call(xa);
} else return JSON.stringify(xa);
}
sa.exports = va;
return sa.exports;
}({}),
p = function(ra) {
var sa = {
exports: ra
};
'use strict';
var ta = 'console',
ua = 'error',
va = 'Facebook Pixel Error',
wa = 'Facebook Pixel Warning',
xa = 'warn',
ya = Object.prototype.toString,
za = !('addEventListener' in b),
ab = function() {},
bb = a[ta] || {},
cb = a.postMessage || ab;
function db(ib) {
return Array.isArray ? Array.isArray(ib) : ya.call(ib) === '[object Array]';
}
function eb(ib) {
cb({
action: 'FB_LOG',
logType: va,
logMessage: ib
}, '*');
if (ua in bb) bb[ua](va + ': ' + ib);
}
function fb(ib) {
cb({
action: 'FB_LOG',
logType: wa,
logMessage: ib
}, '*');
if (xa in bb) bb[xa](wa + ': ' + ib);
}
function gb(ib, jb, kb) {
jb = za ? 'on' + jb : jb;
var lb = za ? 'attachEvent' : 'addEventListener',
mb = za ? 'detachEvent' : 'removeEventListener',
nb = function() {
ib[mb](jb, nb, false);
kb();
};
ib[lb](jb, nb, false);
}
function hb(ib, jb, kb) {
var lb = ib[jb];
ib[jb] = function() {
var mb = lb.apply(this, arguments);
kb.apply(this, arguments);
return mb;
};
}
ra.isArray = db;
ra.logError = eb;
ra.logWarning = fb;
ra.listenOnce = gb;
ra.injectMethod = hb;
return sa.exports;
}({}),
q = function(ra) {
var sa = {
exports: ra
};
'use strict';
var ta = /^[+-]?\d+(\.\d+)?$/,
ua = 'number',
va = 'currency_code',
wa = {
AED: 1,
ARS: 1,
AUD: 1,
BOB: 1,
BRL: 1,
CAD: 1,
CHF: 1,
CLP: 1,
CNY: 1,
COP: 1,
CRC: 1,
CZK: 1,
DKK: 1,
EUR: 1,
GBP: 1,
GTQ: 1,
HKD: 1,
HNL: 1,
HUF: 1,
IDR: 1,
ILS: 1,
INR: 1,
ISK: 1,
JPY: 1,
KRW: 1,
MOP: 1,
MXN: 1,
MYR: 1,
NIO: 1,
NOK: 1,
NZD: 1,
PEN: 1,
PHP: 1,
PLN: 1,
PYG: 1,
QAR: 1,
RON: 1,
RUB: 1,
SAR: 1,
SEK: 1,
SGD: 1,
THB: 1,
TRY: 1,
TWD: 1,
USD: 1,
UYU: 1,
VEF: 1,
VND: 1,
ZAR: 1
},
xa = {
value: {
type: ua,
isRequired: true
},
currency: {
type: va,
isRequired: true
}
},
ya = {
PageView: {},
ViewContent: {},
Search: {},
AddToCart: {},
AddToWishlist: {},
InitiateCheckout: {},
AddPaymentInfo: {},
Purchase: {
validationSchema: xa
},
Lead: {},
CompleteRegistration: {},
CustomEvent: {
validationSchema: {
event: {
isRequired: true
}
}
}
},
za = Object.prototype.hasOwnProperty;
function ab(cb, db) {
this.eventName = cb;
this.params = db || {};
this.error = null;
this.warnings = [];
}
ab.prototype = {
validate: function() {
var cb = this.eventName,
db = ya[cb];
if (!db) return this._error('Unsupported event: ' + cb);
var eb = db.validationSchema;
for (var fb in eb)
if (za.call(eb, fb)) {
var gb = eb[fb];
if (gb.isRequired === true && !za.call(this.params, fb)) return this._error('Required parameter "' + fb + '" is missing for event "' + cb + '"');
if (gb.type)
if (!this._validateParam(fb, gb.type)) return this._error('Parameter "' + fb + '" is invalid for event "' + cb + '"');
}
return this;
},
_validateParam: function(cb, db) {
var eb = this.params[cb];
switch (db) {
case ua:
var fb = ta.test(eb);
if (fb && Number(eb) < 0) this.warnings.push('Parameter "' + cb + '" is negative for event "' + this.eventName + '"');
return fb;
case va:
return wa[eb.toUpperCase()] === 1;
}
return true;
},
_error: function(cb) {
this.error = cb;
return this;
}
};
function bb(cb, db) {
return new ab(cb, db).validate();
}
ra.validateEvent = bb;
return sa.exports;
}({}),
r = null,
s = a.fbq;
if (!s) return p.logError('Pixel code is not installed correctly on this page');
var t = Array.prototype.slice,
u = Object.prototype.hasOwnProperty,
v = c.href,
w = false,
x = false,
y = a.top !== a,
z = [],
aa = {},
ba = b.referrer,
ca = {};
function da(ra) {
for (var sa in ra)
if (u.call(ra, sa)) this[sa] = ra[sa];
}
function ea(ra) {
if (!j.length) {
var sa = t.call(arguments),
ta = sa.length === 1 && p.isArray(sa[0]);
if (ta) sa = sa[0];
if (ra.slice(0, 6) === 'report') {
var ua = ra.slice(6);
if (ua === 'CustomEvent') {
ua = (sa[1] || {}).event || ua;
sa = ['trackCustom', ua].concat(sa.slice(1));
} else sa = ['track', ua].concat(sa.slice(1));
}
ra = sa.shift();
switch (ra) {
case 'addPixelId':
w = true;
return fa.apply(this, sa);
case 'init':
x = true;
return fa.apply(this, sa);
case 'track':
if (m.test(sa[0])) return ia.apply(this, sa);
if (ta) return ha.apply(this, sa);
return ga.apply(this, sa);
case 'trackCustom':
return ha.apply(this, sa);
case 'send':
return ja.apply(this, sa);
default:
p.logError('Invalid or unknown method name "' + ra + '"');
}
} else s.queue.push(arguments);
}
s.callMethod = ea;
function fa(ra, sa) {
if (u.call(aa, ra)) {
p.logError('Duplicate Pixel ID: ' + ra);
return;
}
var ta = {
id: ra,
userData: sa || {}
};
if (sa != null) pa('IDENTITY');
z.push(ta);
aa[ra] = ta;
}
function ga(ra, sa) {
sa = sa || {};
var ta = q.validateEvent(ra, sa);
if (ta.error) p.logError(ta.error);
if (ta.warnings)
for (var ua = 0; ua < ta.warnings.length; ua++) p.logWarning(ta.warnings[ua]);
if (ra === 'CustomEvent') ra = sa.event;
ha.call(this, ra, sa);
}
function ha(ra, sa) {
var ta = this instanceof da ? this : n,
ua = ra === 'PageView';
for (var va = 0, wa = z.length; va < wa; va++) {
var xa = z[va];
if (ua && ta.allowDuplicatePageViews === false && ca[xa.id] === true) continue;
ka(xa, ra, sa);
if (ua) ca[xa.id] = true;
}
}
function ia(ra, sa) {
ka(null, ra, sa);
}
function ja(ra, sa) {
for (var ta = 0, ua = z.length; ta < ua; ta++) ka(z[ta], ra, sa);
}
function ka(ra, sa, ta) {
ra = ra || {};
var ua = new o();
ua.append('id', ra.id);
ua.append('ev', sa);
ua.append('dl', v);
ua.append('rl', ba);
ua.append('if', y);
ua.append('ts', new Date().valueOf());
ua.append('cd', ta);
ua.append('ud', ra.userData);
ua.append('v', e || s.version);
ua.append('a', s.agent);
var va = b.visibilityState;
if (va !== undefined) ua.append('pv', va);
var wa = ua.toQueryString();
if (2048 > (f + '?' + wa).length) {
la(f, wa);
} else ma(f, ua);
}
function la(ra, sa) {
var ta = new Image();
ta.src = ra + '?' + sa;
}
function ma(ra, sa) {
var ta = 'fb' + Math.random().toString().replace('.', ''),
ua = b.createElement('form');
ua.method = 'post';
ua.action = ra;
ua.target = ta;
ua.acceptCharset = 'utf-8';
ua.style.display = 'none';
var va = !!(a.attachEvent && !a.addEventListener),
wa = va ? '<iframe name="' + ta + '">' : 'iframe',
xa = b.createElement(wa);
xa.src = 'javascript:false';
xa.id = ta;
xa.name = ta;
ua.appendChild(xa);
p.listenOnce(xa, 'load', function() {
sa.each(function(ya, za) {
var ab = b.createElement('input');
ab.name = ya;
ab.value = za;
ua.appendChild(ab);
});
p.listenOnce(xa, 'load', function() {
ua.parentNode.removeChild(ua);
});
ua.submit();
});
b.body.appendChild(ua);
}
function na() {
while (s.queue.length && !j.length) {
var ra = s.queue.shift();
ea.apply(s, ra);
}
}
function oa() {
k = b.getElementsByTagName('script');
for (var ra = 0; ra < k.length && !l; ra++) {
var sa = k[ra].src.split(g);
if (sa.length > 1) l = sa[0];
}
}
function pa(ra) {
var sa = h[ra];
if (sa)
if (i[ra]) {
i[ra]({
pixels: z
});
} else if (j.indexOf(ra) === -1) {
if (l == null) oa();
j.push(ra);
var ta = b.createElement('script');
ta.src = l + g + sa;
ta.async = true;
var ua = k[0];
if (ua) ua.parentNode.insertBefore(ta, ua);
}
}
s.loadPlugin = pa;
function qa(ra, sa) {
if (ra && sa) {
i[ra] = sa;
sa({
pixels: z
});
var ta = j.indexOf(ra);
if (ta > -1) j.splice(ta, 1);
if (!j.length) na();
}
}
s.registerPlugin = qa;
if (s.pixelId) {
w = true;
fa(s.pixelId);
}
na();
if (w && x || a.fbq !== a._fbq) p.logWarning('Multiple pixels with conflicting versions were detected on this page');
if (z.length > 1) p.logWarning('Multiple different pixels were detected on this page');
(function ra() {
if (s.disablePushState === true) return;
if (!d.pushState || !d.replaceState) return;
var sa = function() {
ba = v;
v = c.href;
if (v === ba) return;
var ta = new da({
allowDuplicatePageViews: true
});
ea.call(ta, 'trackCustom', 'PageView');
};
p.injectMethod(d, 'pushState', sa);
p.injectMethod(d, 'replaceState', sa);
a.addEventListener('popstate', sa, false);
})();
})(window, document, location, history);
} catch (e) {
new Image().src = "https:\/\/www.facebook.com\/" + 'common/scribe_endpoint.php?c=jssdk_error&m=' + encodeURIComponent('{"error":"LOAD", "extra": {"name":"' + e.name + '","line":"' + (e.lineNumber || e.line) + '","script":"' + (e.fileName || e.sourceURL || e.script) + '","stack":"' + (e.stackTrace || e.stack) + '","revision":"2361294","namespace":"FB","message":"' + e.message + '"}}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment