Skip to content

Instantly share code, notes, and snippets.

@dawncold
Last active April 23, 2021 15:21
Show Gist options
  • Save dawncold/4b9893d5db7ef41d1a1959b323d61b15 to your computer and use it in GitHub Desktop.
Save dawncold/4b9893d5db7ef41d1a1959b323d61b15 to your computer and use it in GitHub Desktop.
ga and pendo install script explain
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
function installGA() {
window['GoogleAnalyticsObject'] = 'ga';
window['ga'] = window['ga'] || function () {
window['ga'].q = window['ga'].q || [];
window['ga'].q.push(arguments);
};
window['ga'].l = 1 * new Date();
a = document.createElement('script');
a.async = 1;
a.src = 'https://www.google-analytics.com/analytics.js'
m = document.getElementsByTagName('script')[0];
m.parentNode.insertBefore(a, m);
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
};
(function (apiKey) {
(function(p,e,n,d,o){var v,w,x,y,z;o=p[d]=p[d]||{};o._q=[];
v=['initialize','identify','updateOptions','pageLoad'];for(w=0,x=v.length;w<x;++w)(function(m){
o[m]=o[m]||function(){o._q[m===v[0]?'unshift':'push']([m].concat([].slice.call(arguments,0)));};})(v[w]);
y=e.createElement(n);y.async=!0;y.src='https://cdn.pendo.io/agent/static/'+apiKey+'/pendo.js';
z=e.getElementsByTagName(n)[0];z.parentNode.insertBefore(y,z);})(window,document,'script','pendo');
})('<ACTUAL_API_KEY_HERE>');
function installPendo() {
window['pendo'] = window['pendo'] || {};
o = window['pendo'];
o._q = [];
['initialize','identify','updateOptions','pageLoad'].forEach(methodName => {
window['pendo'][methodName] = window['pendo'][methodName] || function () {
if (methodName === 'initialize') {
o._q.unshift(['initialize'].concat([].slice.call(arguments, 0)));
} else {
o._q.push([methodName].concat([].slice.call(arguments, 0)));
}
};
})
y = document.createElement('script');
y.async = 1;
y.src = 'https://cdn.pendo.io/agent/static/YOURAPIKEY/pendo.js';
z = document.getElementsByTagName('script');
z.parentNode.insertBefore(y, z);
pendo.initialize({
visitor: {
id: 1,
other_prop: 2,
another_prop: 3
},
account: {
}
})
}
//in global(window)
function f() {console.log(arguments);console.log(this);}
f(1,2,3)
// -> Arguments(3) [1, 2, 3, callee: ƒ, Symbol(Symbol.iterator): ƒ]
// -> Window {window: Window, self: Window, document: document, name: "", location: Location, …}
// Why `this` points to Window ? Because f(1,2,3) is `window.f(1,2,3)`, `window` will be the `this` in `f`,
// e.g. `abc.func()`, `abc` will be the `this` in `func`
// NEW
// function Product(category){this.category = category;}
// Product('tech') -> window.category is 'tech'
// p = new Product('tech') -> p.category is 'tech'
// CALL / APPLY
// Array.prototype.slice.call(arguments) Or [].slice.call(arguments)
// [1,2,3].slice -> [1,2,3] will be the `this` in `slice`
// But call / apply are different, they can change `this` for the method
// e.g. [].slice.call(arguments) -> call `slice` method on Array, and change the `this` in slice with `arguments`
function f2() {console.log([].slice.call(arguments));}
f2(1,2,3)
// -> [1,2,3] <- this is an array!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment