@sweep/analytics-js
Version:
Vanilla ES6 tracking library for sweep analytics integration.
7 lines (6 loc) • 5.78 kB
JavaScript
/*!
* @sweep/analytics-js v0.1.1
* Copyright (c) 2019-2019 Sweep Analytics
* License: MIT
*/
!function(e){"function"==typeof define&&define.amd?define(e):e()}(function(){"use strict";function e(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function t(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}for(var n=function(e,t){return e(t={exports:{}},t.exports),t.exports}(function(e){var t="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof window.msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto);if(t){var n=new Uint8Array(16);e.exports=function(){return t(n),n}}else{var r=new Array(16);e.exports=function(){for(var e,t=0;t<16;t++)0==(3&t)&&(e=4294967296*Math.random()),r[t]=e>>>((3&t)<<3)&255;return r}}}),r=[],o=0;o<256;++o)r[o]=(o+256).toString(16).substr(1);var a=function(e,t){var n=t||0,o=r;return[o[e[n++]],o[e[n++]],o[e[n++]],o[e[n++]],"-",o[e[n++]],o[e[n++]],"-",o[e[n++]],o[e[n++]],"-",o[e[n++]],o[e[n++]],"-",o[e[n++]],o[e[n++]],o[e[n++]],o[e[n++]],o[e[n++]],o[e[n++]]].join("")};var i=function(e,t,r){var o=t&&r||0;"string"==typeof e&&(t="binary"===e?new Array(16):null,e=null);var i=(e=e||{}).random||(e.rng||n)();if(i[6]=15&i[6]|64,i[8]=63&i[8]|128,t)for(var c=0;c<16;++c)t[o+c]=i[c];return t||a(i)};function c(e){for(var t=e+"=",n=document.cookie.split(";"),r=0;r<n.length;r++){for(var o=n[r];" "===o.charAt(0);)o=o.substring(1,o.length);if(0===o.indexOf(t))return o.substring(t.length,o.length)}return null}var s=function e(t,n,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),window.sweep={sweepApiKey:t,sweepLogs:n,sweepNoCookie:r}},p=function(){return"mutation trackEvent($type: EventType!, $name: String!, $client: String!, $meta: JSON) {\n trackEvent(type: $type, input: { name: $name, client: $client, meta: $meta }) { \n name,\n client,\n meta\n }\n}"};function u(){var e=window.sweep.sweepNoCookie;"true"===e||c("s_a_js_uid")||function(e,t){var n=new Date;n.setTime(n.getTime()+31536e6);var r="; expires="+n.toUTCString();document.cookie=e+"="+(t||"")+r+"; path=/"}("s_a_js_uid",i());try{var t=window.sweep.sweepApiKey;if(!t)throw new Error("No api key provided");var n=window.navigator,r=window.location,o=n.userAgent;if(o.search(/(bot|spider|crawl)/gi)>-1)throw new Error("bot… not tracked");var a,s="",u={url:a=r.hash?r.protocol+"//"+r.hostname+r.pathname+r.hash:r.protocol+"//"+r.hostname+r.pathname,referrer:document.referrer,language:navigator.language,platform:navigator.platform,userAgent:o,screen:"".concat(window.screen.width,"x").concat(window.screen.height)};"true"!==e&&(u.anonymousId=c("s_a_js_uid"));var l={method:"post",headers:{"Content-Type":"application/json"},body:JSON.stringify({operationName:"trackEvent",query:p(),variables:{type:"ANALYSIS",name:"userSession",client:t,meta:u}})};if(s===a)return;s=a,fetch("https://api.sweep-analytics.com/public",l).then(function(){console.log("send")}).catch(function(e){throw new Error(e)})}catch(e){console.error(e)}}var l,f={key:(l=document.currentScript).getAttribute("key"),logs:l.getAttribute("logs"),noCookie:l.getAttribute("noCookie")};document.addEventListener("DOMContentLoaded",function(){new s(f.key,f.logs,f.noCookie);d(),[].slice.call(document.querySelectorAll("[data-sweep-click]")).forEach(function(n){n.addEventListener("click",function(n){var r=n.target.getAttribute("data-sweep-click");w("click",function(n){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?t(o,!0).forEach(function(t){e(n,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):t(o).forEach(function(e){Object.defineProperty(n,e,Object.getOwnPropertyDescriptor(o,e))})}return n}({},r.split(",")))})}),"true"===f.logs&&h()});var w=function(e,t){!function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};try{var n=window.sweep.sweepApiKey;if(!n)throw new Error("No api key provided");var r=window.navigator,o=window.location;if(r.userAgent.search(/(bot|spider|crawl)/gi)>-1)throw new Error("bot… not tracked");t.path=o.pathname;var a={method:"post",headers:{"Content-Type":"application/json"},body:JSON.stringify({operationName:"trackEvent",query:p(),variables:{type:"ANALYSIS",name:e,client:n,meta:t}})};fetch("https://api.sweep-analytics.com/public",a).then(function(){console.log("send event")}).catch(function(e){throw new Error(e)})}catch(e){console.error(e)}}(e,t)},d=function(){u()},h=function(){!function(){try{var e=window.sweep.logs;if("true"!==e||!e)return;var t=window.sweep.sweepApiKey;if(!t)throw new Error("No api key provided");var n=window.navigator,r=window.location,o=n.userAgent;if(o.search(/(bot|spider|crawl)/gi)>-1)throw new Error("bot… not tracked");var a={url:r.hash?r.protocol+"//"+r.hostname+r.pathname+r.hash:r.protocol+"//"+r.hostname+r.pathname,referrer:document.referrer,language:navigator.language,platform:navigator.platform,userAgent:o,screen:"".concat(window.screen.width,"x").concat(window.screen.height)};window.addEventListener("error",function(e){var n={line:e.lineno,filename:e.filename,message:e.message,error:e.error};a.log=n;var r={method:"post",headers:{"Content-Type":"application/json"},body:JSON.stringify({operationName:"trackEvent",query:p(),variables:{type:"LOG",name:e,client:t,meta:a}})};fetch("https://api.sweep-analytics.com/public",r).then(function(){console.log("send event")}).catch(function(e){throw new Error(e)})})}catch(e){console.error(e)}}()}});