Skip to content

Instantly share code, notes, and snippets.

@broerjuang
Created July 15, 2022 05:51
Show Gist options
  • Save broerjuang/d34e673deb0c97588d280749cfb21915 to your computer and use it in GitHub Desktop.
Save broerjuang/d34e673deb0c97588d280749cfb21915 to your computer and use it in GitHub Desktop.
var analyticsTrackable=function(){"use strict";"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var e,n={};(function(e){function n(e,n){this.browser=e,this.os=n}function t(e,n){function t(e){if(e.length>0){var t=e.split("/"),r=t.length>1?t[1]:"";n(t[0],r)}}for(var r="",i=0;i<e.length;++i){var o=e[i];if(" "===o){for(t(r),r="";" "===e[i];)++i;o=e[i]}if("("===o){t(r),r="";var a="";for(++i;i<e.length&&")"!==(o=e[i]);++i)a+=o;for(var s=a.split(";"),u=0;u<s.length;++u)n(s[u].trim());++i}else" "!==o&&"("!==o&&(r+=o)}t(r)}function r(e){var n={"Windows NT 10.0":"10","Windows NT 6.3":"8.1","Windows NT 6.2":"8","Windows NT 6.1":"7","Windows NT 6.0":"Vista","Windows NT 5.2":"XP","Windows NT 5.1":"XP","Windows NT 5.0":"2000",10.4:"Tiger",10.5:"Leopard",10.6:"Snow Leopard",10.7:"Lion",10.8:"Mountain Lion",10.9:"Mavericks","10.10":"Yosemite",10.11:"El Capitan",10.12:"Sierra",10.13:"High Sierra",10.14:"Mojave"};function t(e){var t=n[e];return t||null}function r(e){var t=e.substr(e.indexOf("OS X")+5),r=t.indexOf("_")>1?t.split("_"):t.split(".");return r=r.length>=2?r.slice(0,2):r.concat([0]),n[r.join(".")]?r.join(".")+" ("+n[r.join(".")]+")":r.join(".")}function i(e){return e.substr(8).split(".").slice(0,2).join(".")}function o(e){return e.substr(e.indexOf(" OS ")+4).replace(" like Mac OS X","").split("_").slice(0,2).join(".")}var a=[{tag:"Windows NT",name:"Windows",fn:t},{tag:"Intel Mac OS",name:"macOS",fn:r},{tag:"PPC Mac OS",name:"macOS",fn:r},{tag:"Android",name:"Android",fn:i},{tag:"CPU iPhone OS",name:"iOS",fn:o},{tag:"CPU OS",name:"iOS",fn:o},{tag:"Linux"},{tag:"FreeBSD"},{tag:"OpenBSD"},{tag:"NetBSD"},{tag:"SunOS"},{tag:"CrOS",name:"ChromeOS"}],s={name:null,version:null},u=e.reduce((function(e,n){var t=a.find((function(e){return e.tag===n.substr(0,e.tag.length)}));if(t){var r=t.name?t.name:t.tag;e.name=r.split(" ").shift(),e.version=t.fn?t.fn(n):null}return e}),s);return"macOS"===u.name&&(u.name=parseInt(u.version.replace("10.",""),10)<=11?"OS X":"macOS"),u}function i(e,n){function t(e,n){return e|="Chrome"===n,e="EdgiOS"!==n&&"Edge"!==n&&e}var r=null;if(e.reduce(t,!1)){var i=n.Chrome;r={version:parseInt(i,10),fullVersion:i}}return r}function o(e,n){var t=[{seq:["Gecko","Firefox"],name:"Firefox"},{seq:["AppleWebKit","KHTML, like Gecko","Version","Safari"],name:"Safari",ver:"Version"},{seq:["AppleWebKit","CriOS","Mobile","Safari"],name:"Chrome",ver:"CriOS"},{seq:["AppleWebKit","Mobile","Safari","EdgiOS"],name:"Edge",ver:"EdgiOS"},{seq:["AppleWebKit","Chrome","Safari"],name:"Chrome"},{seq:["AppleWebKit","Version","Chrome","Safari"],name:"Chrome"},{seq:["Chrome","Safari","Edge"],name:"Edge"},{seq:["MSIE 6.0"],name:"Internet Explorer",at:"6.0"},{seq:["MSIE 7.0"],name:"Internet Explorer",at:"7.0"},{seq:["Trident/4.0"],name:"Internet Explorer",at:"8.0"},{seq:["Trident/5.0"],name:"Internet Explorer",at:"9.0"},{seq:["Trident/6.0"],name:"Internet Explorer",at:"10.0"},{seq:["Trident/7.0"],name:"Internet Explorer",at:"11.0"},{seq:["AppleWebKit","Silk","Chrome","Safari"],name:"Silk"},{seq:["Chrome","Safari","Vivaldi"],name:"Vivaldi"},{seq:["Chrome","Safari","OPR"],name:"Opera",ver:"OPR"},{seq:["AppleWebKit","Chrome","Safari","EdgA"],name:"Edge",ver:"EdgA"},{seq:["AppleWebKit","SamsungBrowser","Chrome","Safari"],name:"Samsung Browser",ver:"SamsungBrowser"},{seq:["Chrome","UCBrowser","Safari"],name:"UCBrowser"}],r={name:"Unknown",version:null,fullVersion:null,chromeFamily:null},o=n.Mozilla;if("4.0"===o||"5.0"===o){(r=t.reduce((function(t,r){function i(e,n){return e.push(n-e.pop()),e.push(n),e}function o(e){return e>0}if(r.seq.map((function(n){return e.indexOf(n)})).reduce(i,[0]).every(o)){var a=r.ver?r.ver:r.name,s=r.at?r.at:n[a];t.name=r.name,t.version="Edge"===t.name?parseInt(s,10):parseFloat(s),t.fullVersion=s}return t}),r)).chromeFamily=i(e,n);var a=e.find((function(e){return"Tizen"===e.substr(0,5)}));r.name=a?"Tizen":r.name}return r}function a(e){var n=[];return t(e,(function(e,t){n.push({name:e,version:t})})),n}function s(e){var i=[],a={};return t(e,(function(e,n){i.push(e),"string"==typeof n&&(a[e]=n)})),new n(o(i,a),r(i))}n.prototype.toString=function(){var e=this.browser.name;return this.browser.version&&(e+=" "+this.browser.version),this.os.name&&(e+=" on "+this.os.name),this.os.version&&(e+=" "+this.os.version),e},e.analyze=s,e.scan=a,e.version="0.5.0"})(n);var t=new Uint8Array(16);function r(){if(!e&&!(e="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return e(t)}var i=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function o(e){return"string"==typeof e&&i.test(e)}for(var a=[],s=0;s<256;++s)a.push((s+256).toString(16).substr(1));function u(e,n,t){var i=(e=e||{}).random||(e.rng||r)();if(i[6]=15&i[6]|64,i[8]=63&i[8]|128,n){t=t||0;for(var s=0;s<16;++s)n[t+s]=i[s];return n}return function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,t=(a[e[n+0]]+a[e[n+1]]+a[e[n+2]]+a[e[n+3]]+"-"+a[e[n+4]]+a[e[n+5]]+"-"+a[e[n+6]]+a[e[n+7]]+"-"+a[e[n+8]]+a[e[n+9]]+"-"+a[e[n+10]]+a[e[n+11]]+a[e[n+12]]+a[e[n+13]]+a[e[n+14]]+a[e[n+15]]).toLowerCase();if(!o(t))throw TypeError("Stringified UUID is invalid");return t}(i)}var c=function(e,n){if("string"!=typeof e)throw new TypeError("argument str must be a string");for(var t={},r=n||{},i=e.split(";"),o=r.decode||l,a=0;a<i.length;a++){var s=i[a],u=s.indexOf("=");if(!(u<0)){var c=s.substring(0,u).trim();if(null==t[c]){var f=s.substring(u+1,s.length).trim();'"'===f[0]&&(f=f.slice(1,-1)),t[c]=d(f,o)}}}return t},f=function(e,n,t){var r=t||{},i=r.encode||p;if("function"!=typeof i)throw new TypeError("option encode is invalid");if(!m.test(e))throw new TypeError("argument name is invalid");var o=i(n);if(o&&!m.test(o))throw new TypeError("argument val is invalid");var a=e+"="+o;if(null!=r.maxAge){var s=r.maxAge-0;if(isNaN(s)||!isFinite(s))throw new TypeError("option maxAge is invalid");a+="; Max-Age="+Math.floor(s)}if(r.domain){if(!m.test(r.domain))throw new TypeError("option domain is invalid");a+="; Domain="+r.domain}if(r.path){if(!m.test(r.path))throw new TypeError("option path is invalid");a+="; Path="+r.path}if(r.expires){if("function"!=typeof r.expires.toUTCString)throw new TypeError("option expires is invalid");a+="; Expires="+r.expires.toUTCString()}r.httpOnly&&(a+="; HttpOnly");r.secure&&(a+="; Secure");if(r.sameSite){switch("string"==typeof r.sameSite?r.sameSite.toLowerCase():r.sameSite){case!0:a+="; SameSite=Strict";break;case"lax":a+="; SameSite=Lax";break;case"strict":a+="; SameSite=Strict";break;case"none":a+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}}return a},l=decodeURIComponent,p=encodeURIComponent,m=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;function d(e,n){try{return n(e)}catch(n){return e}}function g(e,n){void 0===n&&(n={});var t=function(e){if(e&&"j"===e[0]&&":"===e[1])return e.substr(2);return e}(e);if(function(e,n){return void 0===n&&(n=!e||"{"!==e[0]&&"["!==e[0]&&'"'!==e[0]),!n}(t,n.doNotParse))try{return JSON.parse(t)}catch(e){}return e}var v=function(){return v=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var i in n=arguments[t])Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i]);return e},v.apply(this,arguments)},h=function(){function e(e,n){var t=this;this.changeListeners=[],this.HAS_DOCUMENT_COOKIE=!1,this.cookies=function(e,n){return"string"==typeof e?c(e,n):"object"==typeof e&&null!==e?e:{}}(e,n),new Promise((function(){t.HAS_DOCUMENT_COOKIE="object"==typeof document&&"string"==typeof document.cookie})).catch((function(){}))}return e.prototype._updateBrowserValues=function(e){this.HAS_DOCUMENT_COOKIE&&(this.cookies=c(document.cookie,e))},e.prototype._emitChange=function(e){for(var n=0;n<this.changeListeners.length;++n)this.changeListeners[n](e)},e.prototype.get=function(e,n,t){return void 0===n&&(n={}),this._updateBrowserValues(t),g(this.cookies[e],n)},e.prototype.getAll=function(e,n){void 0===e&&(e={}),this._updateBrowserValues(n);var t={};for(var r in this.cookies)t[r]=g(this.cookies[r],e);return t},e.prototype.set=function(e,n,t){var r;"object"==typeof n&&(n=JSON.stringify(n)),this.cookies=v(v({},this.cookies),((r={})[e]=n,r)),this.HAS_DOCUMENT_COOKIE&&(document.cookie=f(e,n,t)),this._emitChange({name:e,value:n,options:t})},e.prototype.remove=function(e,n){var t=n=v(v({},n),{expires:new Date(1970,1,1,0,0,1),maxAge:0});this.cookies=v({},this.cookies),delete this.cookies[e],this.HAS_DOCUMENT_COOKIE&&(document.cookie=f(e,"",t)),this._emitChange({name:e,value:void 0,options:n})},e.prototype.addChangeListener=function(e){this.changeListeners.push(e)},e.prototype.removeChangeListener=function(e){var n=this.changeListeners.indexOf(e);n>=0&&this.changeListeners.splice(n,1)},e}();function y(e,n){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!t){if(Array.isArray(e)||(t=function(e,n){if(!e)return;if("string"==typeof e)return S(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);"Object"===t&&e.constructor&&(t=e.constructor.name);if("Map"===t||"Set"===t)return Array.from(e);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return S(e,n)}(e))||n&&e&&"number"==typeof e.length){t&&(e=t);var r=0,i=function(){};return{s:i,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){t=t.call(e)},n:function(){var e=t.next();return a=e.done,e},e:function(e){s=!0,o=e},f:function(){try{a||null==t.return||t.return()}finally{if(s)throw o}}}}function S(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}var O=[{key:"utmSource",selector:"source"},{key:"utmMedium",selector:"medium"},{key:"utmCampaign",selector:"name"},{key:"utmContent",selector:"content"},{key:"utmTerm",selector:"term"}],w=function(e){O.forEach((function(n){var t=n.key,r=n.selector,i=e[r];i?T({key:t,value:null!=i?i:null}):k.remove(t,{domain:x()}),T({key:"marketingId"})}))};function b(e){var n=new Map;if(0!==Object.keys(e).length&&e.constructor===Object)for(var t=0;t<O.length;t++){if(e[O[t].selector]!==E(O[t].key)){w(e);break}}var r,i=y(O);try{for(i.s();!(r=i.n()).done;){var o=r.value,a=E(o.key);a?n.set(o.key,a):n.set(o.key,null)}}catch(e){i.e(e)}finally{i.f()}return k.get("marketingId")?n.set("marketingId",k.get("marketingId")):n.set("marketingId",null),Object.fromEntries(n)}var k=new h,C="__rg_cookie_id__";function E(e){return k.get(e)}function x(){var e="undefined"!=typeof window?window.location.hostname:null;return null==e?void 0:e.substring(null==e?void 0:e.indexOf("."))}function T(e){var n=e.key,t=e.value,r=e.maxAge,i=void 0===r?604800:r,o="marketingId"===n?u():t;return k.set(n,o,{maxAge:n!==C?i:void 0,path:"/",domain:x()}),o}function I(e){var n=e.key,t=e.value,r=E(n);return r||(r=T({key:n,value:t})),r}var A="__tracker_session_id__";function j(){var e=k.get(A)||u();return T({key:A,value:e,maxAge:1200}),e}function _(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,r)}return t}function D(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?_(Object(t),!0).forEach((function(n){M(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):_(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function M(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return{name:"trackable",config:e,initializeEnd:function(e){var t=e.instance,r=e.config,i=t.getState(),o=n.analyze(i.context.userAgent);i.context.marketing=b(i.context.campaign,r.isDev),i.context.cookiesId=I({key:C,value:u()}),i.context.isLogged="true"===E("isLoggedIn"),i.context.userID=E("userID"),i.context.session=j(),i.context.clientDevice=o.toString(),i.context.clientOSVersion=o.os.version},track:function(e){var n,t=e.payload,r=e.instance,i=e.config,o=r.getState(),a=o.context;window&&(n=window.location.hostname+window.location.pathname);var s=D(D({},o.context.marketing),{},{source:i.source,appVersion:i.appVersion||a.userAgent,clientOS:a.os.name,clientDevice:a.clientDevice,clientOSVersion:a.clientOSVersion,clientUA:i.clientUA||a.userAgent,clientTimestamp:(new Date).toISOString(),memberId:a.userID,cookiesId:a.cookiesId,sessionId:j(),isLogged:a.isLogged,eventType:t.event,context:JSON.stringify(t.properties),jsonContext:t.properties,originUrl:n});console.log({data:s})}}}}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment