Skip to content

Instantly share code, notes, and snippets.

@harimau99
Created August 14, 2018 05:06
Show Gist options
  • Save harimau99/edde7edda01c932ed9b7e851247da7a9 to your computer and use it in GitHub Desktop.
Save harimau99/edde7edda01c932ed9b7e851247da7a9 to your computer and use it in GitHub Desktop.
try {
(function(win, d, location, history) {
/**
* @param {Object} params
* @return {undefined}
*/
function Document(params) {
var e;
for (e in params) {
if (jQuery.call(params, e)) {
this[e] = params[e];
}
}
}
/**
* @param {string} action
* @return {?}
*/
function process(action) {
if (!classes.length) {
/** @type {Array.<?>} */
var args = __slice.call(arguments);
var va = args.length === 1 && util.isArray(args[0]);
if (va) {
args = args[0];
}
if (action.slice(0, 6) === "report") {
var e = action.slice(6);
if (e === "CustomEvent") {
e = (args[1] || {}).event || e;
/** @type {Array} */
args = ["trackCustom", e].concat(args.slice(1));
} else {
/** @type {Array} */
args = ["track", e].concat(args.slice(1));
}
}
action = args.shift();
switch(action) {
case "addPixelId":
/** @type {boolean} */
a = true;
return fn.apply(this, args);
case "init":
/** @type {boolean} */
b = true;
return fn.apply(this, args);
case "set":
return onError.apply(this, args);
case "track":
if (rchecked.test(args[0])) {
return handler.apply(this, args);
}
if (va) {
return listener.apply(this, args);
}
return trigger.apply(this, args);
case "trackCustom":
return listener.apply(this, args);
case "send":
return wrapper.apply(this, args);
default:
util.logError('Invalid or unknown method name "' + action + '"');
}
} else {
self.queue.push(arguments);
}
}
/**
* @param {string} name
* @param {Object} range
* @param {Object} req
* @return {undefined}
*/
function fn(name, range, req) {
if (jQuery.call(elems, name)) {
util.logError("Duplicate Pixel ID: " + name);
return;
}
var node = {
agent : !!req && req.agent,
id : name,
userData : range || {}
};
data.push(node);
elems[name] = node;
if (range != null) {
next("IDENTITY");
}
}
/**
* @param {boolean} request
* @param {string} cause
* @param {string} name
* @return {undefined}
*/
function onError(request, cause, name) {
var test = handle.validateMetadata(request);
if (test.error) {
util.logError(test.error);
}
if (test.warnings) {
/** @type {number} */
var i = 0;
for (;i < test.warnings.length;i++) {
util.logWarning(test.warnings[i]);
}
}
if (jQuery.call(elems, name)) {
/** @type {number} */
var j = 0;
/** @type {number} */
var pl = data.length;
for (;j < pl;j++) {
if (data[j].id === name) {
/** @type {string} */
data[j][request] = cause;
break;
}
}
} else {
util.logWarning("Trying to set argument " + cause + " for uninitialized pixel ID " + name);
}
}
/**
* @param {string} event
* @param {Object} data
* @return {undefined}
*/
function trigger(event, data) {
data = data || {};
var test = handle.validateEvent(event, data);
if (test.error) {
util.logError(test.error);
}
if (test.warnings) {
/** @type {number} */
var i = 0;
for (;i < test.warnings.length;i++) {
util.logWarning(test.warnings[i]);
}
}
if (event === "CustomEvent") {
event = data.event;
}
listener.call(this, event, data);
}
/**
* @param {string} type
* @param {?} callback
* @return {undefined}
*/
function listener(type, callback) {
var root = this instanceof Document ? this : doc;
/** @type {boolean} */
var isCSS = type === "PageView";
/** @type {number} */
var i = 0;
/** @type {number} */
var iLen = data.length;
for (;i < iLen;i++) {
var error = data[i];
if (isCSS && (root.allowDuplicatePageViews === false && dupMatcher[error.id] === true)) {
continue;
}
remove(error, type, callback);
if (isCSS) {
/** @type {boolean} */
dupMatcher[error.id] = true;
}
}
}
/**
* @param {?} type
* @param {?} callback
* @return {undefined}
*/
function handler(type, callback) {
remove(null, type, callback);
}
/**
* @param {Object} options
* @param {?} args
* @param {?} node
* @return {?}
*/
function init(options, args, node) {
options = options || {};
var t = new Node;
t.append("id", options.id);
t.append("ev", args);
t.append("dl", "https://brickellmensproducts.com/blogs/grooming-manual/63517059-mens-skin-care-routine"); // "dl", url)
t.append("rl", ""); // "rl", hash)
t.append("if", false); // "if", foo)
t.append("ts", (new Date).valueOf());
t.append("cd", node);
t.append("ud", options.userData);
t.append("v", actual || self.version);
t.append("a", options.agent || self.agent);
return t;
}
/**
* @param {?} type
* @param {?} callback
* @return {undefined}
*/
function wrapper(type, callback) {
/** @type {number} */
var i = 0;
/** @type {number} */
var iLen = data.length;
for (;i < iLen;i++) {
remove(data[i], type, callback);
}
}
/**
* @param {Object} cb
* @param {?} keepData
* @param {?} selector
* @return {undefined}
*/
function remove(cb, keepData, selector) {
var ret = init(cb, keepData, selector);
var name = ret.toQueryString();
if (2048 > (id + "?" + name).length) {
onComplete(id, name);
} else {
load(id, ret);
}
}
/**
* @param {string} path
* @param {string} fileName
* @return {undefined}
*/
function onComplete(path, fileName) {
/** @type {Image} */
var image = new Image;
/** @type {string} */
image.src = path + "?" + fileName;
}
/**
* @param {string} url
* @param {?} els
* @return {undefined}
*/
function load(url, els) {
/** @type {string} */
var id = "fb" + Math.random().toString().replace(".", "");
/** @type {Element} */
var form = d.createElement("form");
/** @type {string} */
form.method = "post";
/** @type {string} */
form.action = url;
/** @type {string} */
form.target = id;
/** @type {string} */
form.acceptCharset = "utf-8";
/** @type {string} */
form.style.display = "none";
/** @type {boolean} */
var hasBody = !!(win.attachEvent && !win.addEventListener);
/** @type {string} */
var data = hasBody ? '<iframe name="' + id + '">' : "iframe";
/** @type {Element} */
var field = d.createElement(data);
/** @type {string} */
field.src = "javascript:false";
/** @type {string} */
field.id = id;
/** @type {string} */
field.name = id;
form.appendChild(field);
util.listenOnce(field, "load", function() {
els.each(function(v, val) {
/** @type {Element} */
var inp = d.createElement("input");
/** @type {string} */
inp.name = v;
/** @type {string} */
inp.value = val;
form.appendChild(inp);
});
util.listenOnce(field, "load", function() {
form.parentNode.removeChild(form);
});
form.submit();
});
d.body.appendChild(form);
}
/**
* @return {undefined}
*/
function tick() {
for (;self.queue.length && !classes.length;) {
var args = self.queue.shift();
process.apply(self, args);
}
}
/**
* @return {undefined}
*/
function run() {
/** @type {NodeList} */
scripts = d.getElementsByTagName("script");
/** @type {number} */
var i = 0;
for (;i < scripts.length && !elem;i++) {
var seed = scripts[i].src.split(index);
if (seed.length > 1) {
elem = seed[0];
}
}
}
/**
* @param {string} name
* @return {undefined}
*/
function next(name) {
var ref = testSource[name];
if (ref) {
if (normalized[name]) {
normalized[name]({
pixels : data
});
} else {
if (classes.indexOf(name) === -1) {
if (elem == null) {
run();
}
classes.push(name);
/** @type {Element} */
var tag = d.createElement("script");
tag.src = elem + index + ref;
/** @type {boolean} */
tag.async = true;
var s = scripts[0];
if (s) {
s.parentNode.insertBefore(tag, s);
}
}
}
}
}
/**
* @param {boolean} name
* @param {boolean} fn
* @return {undefined}
*/
function action(name, fn) {
if (name && fn) {
/** @type {boolean} */
normalized[name] = fn;
fn({
pixels : data
});
/** @type {number} */
var index = classes.indexOf(name);
if (index > -1) {
classes.splice(index, 1);
}
if (!classes.length) {
tick();
}
}
}
/** @type {string} */
var actual = "2.5.0";
/** @type {string} */
var id = "https://www.facebook.com/tr/";
/** @type {string} */
var index = "/fbevents.";
var testSource = {
IDENTITY : "plugins.identity.js"
};
var normalized = {};
/** @type {Array} */
var classes = [];
/** @type {null} */
var scripts = null;
/** @type {null} */
var elem = null;
/** @type {RegExp} */
var rchecked = /^\d+$/;
var doc = {
allowDuplicatePageViews : false
};
var Node = function(opt_attributes) {
/**
* @return {undefined}
*/
function Rules() {
/** @type {Array} */
this.list = [];
}
/**
* @param {?} o
* @return {?}
*/
function objectToString(o) {
if (typeof JSON === "undefined" || (JSON === null || !JSON.stringify)) {
return Object.prototype.toString.call(o);
} else {
return JSON.stringify(o);
}
}
var module = {
exports : opt_attributes
};
"use strict";
/** @type {string} */
var r20 = "deep";
/** @type {string} */
var rreturn = "shallow";
Rules.prototype = {
/**
* @param {string} name
* @param {?} key
* @return {undefined}
*/
append : function has(name, key) {
this._append(encodeURIComponent(name), key, r20);
},
/**
* @param {string} obj
* @param {?} object
* @param {string} regex
* @return {undefined}
*/
_append : function keys(obj, object, regex) {
if (Object(object) !== object) {
this._appendPrimitive(obj, object);
} else {
if (regex === r20) {
this._appendObject(obj, object);
} else {
this._appendPrimitive(obj, objectToString(object));
}
}
},
/**
* @param {string} a
* @param {?} item
* @return {undefined}
*/
_appendPrimitive : function add(a, item) {
if (item != null) {
this.list.push([a, item]);
}
},
/**
* @param {string} prefix
* @param {Object} object
* @return {undefined}
*/
_appendObject : function serialize(prefix, object) {
var key;
for (key in object) {
if (object.hasOwnProperty(key)) {
/** @type {string} */
var suiteView = prefix + "[" + encodeURIComponent(key) + "]";
this._append(suiteView, object[key], rreturn);
}
}
},
/**
* @param {Function} onComplete
* @return {undefined}
*/
each : function finish(onComplete) {
var list = this.list;
/** @type {number} */
var i = 0;
var listLength = list.length;
for (;i < listLength;i++) {
onComplete(list[i][0], list[i][1]);
}
},
/**
* @return {?}
*/
toQueryString : function flatten() {
/** @type {Array} */
var tagNameArr = [];
this.each(function(k, v) {
tagNameArr.push(k + "=" + encodeURIComponent(v));
});
return tagNameArr.join("&");
}
};
/** @type {function (): undefined} */
module.exports = Rules;
return module.exports;
}({});
var util = function(exports) {
/**
* @param {?} o
* @return {?}
*/
function isArray(o) {
return Array.isArray ? Array.isArray(o) : ostring.call(o) === "[object Array]";
}
/**
* @param {string} message
* @return {undefined}
*/
function log(message) {
callback({
action : "FB_LOG",
logType : mySecret,
logMessage : message
}, "*");
if (type in obj) {
obj[type](mySecret + ": " + message);
}
}
/**
* @param {string} evt
* @return {undefined}
*/
function close(evt) {
callback({
action : "FB_LOG",
logType : logType,
logMessage : evt
}, "*");
if (key in obj) {
obj[key](logType + ": " + evt);
}
}
/**
* @param {Object} view
* @param {string} evt
* @param {Function} comparator
* @return {undefined}
*/
function remove(view, evt, comparator) {
evt = old ? "on" + evt : evt;
/** @type {string} */
var name = old ? "attachEvent" : "addEventListener";
/** @type {string} */
var method = old ? "detachEvent" : "removeEventListener";
/**
* @return {undefined}
*/
var value = function value() {
view[method](evt, value, false);
comparator();
};
view[name](evt, value, false);
}
/**
* @param {Object} event
* @param {string} methodName
* @param {Function} fn
* @return {undefined}
*/
function create(event, methodName, fn) {
var func = event[methodName];
/**
* @return {?}
*/
event[methodName] = function() {
var props = func.apply(this, arguments);
fn.apply(this, arguments);
return props;
};
}
var module = {
exports : exports
};
"use strict";
/** @type {string} */
var headVar = "console";
/** @type {string} */
var type = "error";
/** @type {string} */
var mySecret = "Facebook Pixel Error";
/** @type {string} */
var logType = "Facebook Pixel Warning";
/** @type {string} */
var key = "warn";
/** @type {function (this:*): string} */
var ostring = Object.prototype.toString;
/** @type {boolean} */
var old = !("addEventListener" in d);
/**
* @return {undefined}
*/
var fn = function kb() {
};
var obj = win[headVar] || {};
/** @type {function (this:Window, *, (Array|null|string), (Array|null|string)=): ?} */
var callback = win.postMessage || fn;
/** @type {function (?): ?} */
exports.isArray = isArray;
/** @type {function (string): undefined} */
exports.logError = log;
/** @type {function (string): undefined} */
exports.logWarning = close;
/** @type {function (Object, string, Function): undefined} */
exports.listenOnce = remove;
/** @type {function (Object, string, Function): undefined} */
exports.injectMethod = create;
return module.exports;
}({});
var handle = function(opt_attributes) {
/**
* @param {string} evt
* @param {Object} params
* @param {Object} allBindingsAccessor
* @return {undefined}
*/
function init(evt, params, allBindingsAccessor) {
/** @type {Object} */
this.metadata = allBindingsAccessor;
/** @type {string} */
this.eventName = evt;
this.params = params || {};
/** @type {null} */
this.error = null;
/** @type {Array} */
this.warnings = [];
}
/**
* @param {boolean} context
* @return {?}
*/
function jQuery(context) {
return(new init(null, null, context)).validateMetadata();
}
/**
* @param {string} type
* @param {Function} elem
* @return {?}
*/
function _queueHooks(type, elem) {
return(new init(type, elem)).validateEvent();
}
var module = {
exports : opt_attributes
};
"use strict";
/** @type {RegExp} */
var rbrace = /^[+-]?\d+(\.\d+)?$/;
/** @type {string} */
var NUMBER = "number";
/** @type {string} */
var e = "currency_code";
var data = {
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
};
var activeBackdrops = {
value : {
type : NUMBER,
isRequired : true
},
currency : {
type : e,
isRequired : true
}
};
var subscribers = {
PageView : {},
ViewContent : {},
Search : {},
AddToCart : {},
AddToWishlist : {},
InitiateCheckout : {},
AddPaymentInfo : {},
Purchase : {
validationSchema : activeBackdrops
},
Lead : {},
CompleteRegistration : {},
CustomEvent : {
validationSchema : {
event : {
isRequired : true
}
}
}
};
var args = {
agent : {}
};
/** @type {function (this:Object, *): boolean} */
var plugins = Object.prototype.hasOwnProperty;
init.prototype = {
/**
* @return {?}
*/
validateMetadata : function init() {
var idx = this.metadata.toLowerCase();
var next = args[idx];
if (!next) {
return this._error("Unsupported metadata argument: " + idx);
}
return this;
},
/**
* @return {?}
*/
validateEvent : function wrapper() {
var eventName = this.eventName;
var eventSubscribers = subscribers[eventName];
if (!eventSubscribers) {
this.warnings.push("You are sending a non-standard event '" + eventName + "'. The " + "preferred way to send events is using trackCustom. See " + "https://www.facebookmarketingdevelopers.com/pixels/up#sec-custom " + "for more information");
return this;
}
var directive = eventSubscribers.validationSchema;
var sel;
for (sel in directive) {
if (plugins.call(directive, sel)) {
var rule = directive[sel];
if (rule.isRequired === true && !plugins.call(this.params, sel)) {
return this._error('Required parameter "' + sel + '" is missing for event "' + eventName + '"');
}
if (rule.type) {
if (!this._validateParam(sel, rule.type)) {
return this._error('Parameter "' + sel + '" is invalid for event "' + eventName + '"');
}
}
}
}
return this;
},
/**
* @param {string} p
* @param {?} type
* @return {?}
*/
_validateParam : function parse(p, type) {
var val = this.params[p];
switch(type) {
case NUMBER:
/** @type {boolean} */
var res = rbrace.test(val);
if (res && Number(val) < 0) {
this.warnings.push('Parameter "' + p + '" is negative for event "' + this.eventName + '"');
}
return res;
case e:
return data[val.toUpperCase()] === 1;
}
return true;
},
/**
* @param {string} err
* @return {?}
*/
_error : function done(err) {
/** @type {string} */
this.error = err;
return this;
}
};
/** @type {function (boolean): ?} */
opt_attributes.validateMetadata = jQuery;
/** @type {function (string, Function): ?} */
opt_attributes.validateEvent = _queueHooks;
return module.exports;
}({});
/** @type {null} */
var r = null;
var self = win.fbq;
if (!self) {
return util.logError("Pixel code is not installed correctly on this page");
}
/** @type {function (this:(Array.<T>|string|{length: number}), *=, *=): Array.<T>} */
var __slice = Array.prototype.slice;
/** @type {function (this:Object, *): boolean} */
var jQuery = Object.prototype.hasOwnProperty;
/** @type {string} */
var url = location.href;
/** @type {boolean} */
var a = false;
/** @type {boolean} */
var b = false;
/** @type {boolean} */
var foo = win.top !== win;
/** @type {Array} */
var data = [];
var elems = {};
/** @type {string} */
var hash = location.href;
var dupMatcher = {};
/** @type {function (string): ?} */
self.callMethod = process;
/** @type {function (string): undefined} */
self.loadPlugin = next;
/** @type {function (boolean, boolean): undefined} */
self.registerPlugin = action;
if (self.pixelId) {
/** @type {boolean} */
a = true;
fn(self.pixelId);
}
tick();
if (a && b || win.fbq !== win._fbq) {
util.logWarning("Multiple pixels with conflicting versions were detected on this page");
}
if (data.length > 1) {
util.logWarning("Multiple different pixels were detected on this page");
}
(function init() {
if (self.disablePushState === true) {
return;
}
if (!history.pushState || !history.replaceState) {
return;
}
/**
* @return {undefined}
*/
var fn = function initialize() {
hash = url;
/** @type {string} */
url = location.href;
if (url === hash) {
return;
}
var ret = new Document({
allowDuplicatePageViews : true
});
process.call(ret, "trackCustom", "PageView");
};
util.injectMethod(history, "pushState", fn);
util.injectMethod(history, "replaceState", fn);
win.addEventListener("popstate", fn, false);
})();
})(window, document, location, history);
} catch (e$$13) {
/** @type {string} */
(new Image).src = "https://www.facebook.com/" + "common/scribe_endpoint.php?c=jssdk_error&m=" + encodeURIComponent('{"error":"LOAD", "extra": {"name":"' + e$$13.name + '","line":"' + (e$$13.lineNumber || e$$13.line) + '","script":"' + (e$$13.fileName || (e$$13.sourceURL || e$$13.script)) + '","stack":"' + (e$$13.stackTrace || e$$13.stack) + '","revision":"2665999","namespace":"FB","message":"' + e$$13.message + '"}}');
}
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment