forked from john-doherty/mixpanel-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixpanel-lite.min.js
8 lines (8 loc) · 7.64 KB
/
mixpanel-lite.min.js
1
2
3
4
5
6
7
8
/*!
* mixpanel-lite.js - v1.3.0
* Lightweight version of mixpanel-js with offline support
* https://github.com/john-doherty/mixpanel-lite
* @author John Doherty <www.johndoherty.info>
* @license MIT
*/
!function(e,t){"use strict";if(e.localStorage)if(e.Promise){var n="https://api.mixpanel.com/track?ip=1&verbose=1&data=",i="https://api.mixpanel.com/engage?ip=1&verbose=1&data=",o=null,r=!1,a="1"===String(navigator.doNotTrack||"0"),s={},l={_key:"mixpanel-lite",all:function(){return JSON.parse(localStorage.getItem(l._key)||"[]")},add:function(e){var t=l.all();(function(e,t,n){e=JSON.parse(JSON.stringify(e||{})),t=JSON.parse(JSON.stringify(t||{}));for(var i=0,o=(n=n||[]).length;i<o;i++)g(e,n[i]),g(t,n[i]);return JSON.stringify(e)===JSON.stringify(t)})(e,t.slice(-1)[0]||{},["properties.time","properties.$insert_id"])||(t.push(e),localStorage.setItem(l._key,JSON.stringify(t)))},clear:function(){localStorage.setItem(l._key,JSON.stringify([]))},reset:function(e){localStorage.setItem(l._key,JSON.stringify(e||[]))}},c={init:function(e,t){console.log("mixpanel.track('"+e+"',"+JSON.stringify(t||{})+")")},track:function(e,t){console.log("mixpanel.track('"+e+"',"+JSON.stringify(t||{})+")")},reset:function(){console.log("mixpanel.reset()")},identify:function(e){console.log("mixpanel.identify('"+e+"')")},people:{set:function(e){console.log("mixpanel.people.set("+JSON.stringify(e||{})+")")}},mute:v,unmute:h,muted:!0},d={init:S,track:u,reset:function(){S(o),r&&console.log("mixpanel.reset()")},identify:function(e){e&&""!==e.trim()?(r&&console.log("mixpanel.identify('"+e+"')"),u("$identify",{$anon_distinct_id:s.distinct_id,distinct_id:e,$user_id:e}),s.distinct_id=e,s.$user_id=e):console.warn("mixpanel.identify: Invalid id")},people:{set:function(e){if(e&&"object"==typeof e)if(a)console.warn("mixpanel.track: user does not want to be tracked");else{var t={$token:o,$distinct_id:s.distinct_id,$set:{}};Object.keys(e||{}).forEach(function(n){t.$set[n]=e[n]}),Object.keys(t.$set).forEach(function(e){null!==t.$set[e]&&""!==t.$set[e]||delete t.$set[e]}),l.add(t),f(),r&&(console.log("mixpanel.people.set("+JSON.stringify(e||{})+")"),console.dir(t))}else console.warn("mixpanel.setPeople: Invalid data param, must be an object")}},mute:v,unmute:h,muted:!1};e.mixpanel=d,e.addEventListener(e.cordova?"deviceready":"load",f,{passive:!0}),e.addEventListener("online",f,{passive:!0})}else console.warn("mixpanel-lite: Promise not supported (try adding a polyfill)");else console.warn("mixpanel-lite: localStorage not supported");function p(){var e=navigator.connection||navigator.mozConnection||navigator.webkitConnection||{};return e.effectiveType||e.type||""}function u(e,t){if(o)if(e&&""!==e)if(t&&"object"!=typeof t)console.warn("mixpanel.track: Data param must be an object");else if(a)console.warn("mixpanel.track: user does not want to be tracked");else{var n,i={event:e,properties:(n=s,JSON.parse(JSON.stringify(n)))};Object.keys(t||{}).forEach(function(e){i.properties[e]=t[e]}),i.properties.$insert_id=Math.random().toString(36).substring(2,10)+Math.random().toString(36).substring(2,10),i.properties.time=Math.round((new Date).getTime()/1e3),Object.keys(i.properties).forEach(function(e){null!==i.properties[e]&&""!==i.properties[e]||delete i.properties[e]}),l.add(i),f(),r&&(t?console.log("mixpanel.track('"+e+"',"+JSON.stringify(t||{})+")"):console.log("mixpanel.track('"+e+"')"),console.dir(i))}else console.warn("mixpanel.track: Invalid eventName");else console.warn("mixpanel.track: You must call mixpanel.init(token) first")}function f(){var t,o,r=l.all().filter(function(e){return!e.__completed}),a=r.map(function(t){return function(){var o,r=t.$set?i:n,a=(o=JSON.stringify(t),e.btoa(unescape(encodeURIComponent(o))));return r+=encodeURIComponent(a)+"&_="+(new Date).getTime(),t.__completed=!1,function(e){return new Promise(function(t,n){var i=new XMLHttpRequest;i.open("GET",e),i.withCredentials=!0,i.onreadystatechange=function(){4===i.readyState&&(200===i.status?t({url:e,status:200,body:i.responseText||""}):n({url:e,status:i.status,body:i.responseText||""}))},i.send()})}(r).then(function(){t.__completed=!0})}});(t=a,o=Promise.resolve(),t.forEach(function(e){o=o.then(e)}),o).then(function(){var e=r.filter(function(e){return!e.__completed});l.reset(e)})}function g(e,t){if(e&&t){"string"==typeof t&&(t=t.split("."));for(var n=0;n<t.length-1;n++)if(void 0===(e=e[t[n]]))return;delete e[t.pop()]}}function m(){var t=navigator.userAgent,n=navigator.vendor||"";function i(e,t){return-1!==e.indexOf(t)}return e.opera||i(t," OPR/")?i(t,"Mini")?"Opera Mini":"Opera":/(BlackBerry|PlayBook|BB10)/i.test(t)?"BlackBerry":i(t,"IEMobile")||i(t,"WPDesktop")?"Internet Explorer Mobile":i(t,"Edge")?"Microsoft Edge":i(t,"FBIOS")?"Facebook Mobile":i(t,"Chrome")?"Chrome":i(t,"CriOS")?"Chrome iOS":i(t,"UCWEB")||i(t,"UCBrowser")?"UC Browser":i(t,"FxiOS")?"Firefox iOS":i(n,"Apple")?i(t,"Mobile")?"Mobile Safari":"Safari":i(t,"Android")?"Android Mobile":i(t,"Konqueror")?"Konqueror":i(t,"Firefox")?"Firefox":i(t,"MSIE")||i(t,"Trident/")?"Internet Explorer":i(t,"Gecko")?"Mozilla":""}function v(){e.mixpanel=c}function h(){e.mixpanel=d}function S(n,i){if(!0===(i||{}).mute&&(e.mixpanel=c),""!==(n=String(n||""))){o=n,r=!0===(i||{}).debug;var a,l,d,u,f=(a=function(){for(var e=1*new Date,t=0;e==1*new Date;)t++;return e.toString(16)+t.toString(16)},l=(screen.height*screen.width).toString(16),a()+"-"+Math.random().toString(16).replace(".","")+"-"+function(){var e,t,n=navigator.userAgent,i=[],o=0;function r(e,t){var n,o=0;for(n=0;n<t.length;n++)o|=i[n]<<8*n;return e^o}for(e=0;e<n.length;e++)t=n.charCodeAt(e),i.unshift(255&t),i.length>=4&&(o=r(o,i),i=[]);return i.length>0&&(o=r(o,i)),o.toString(16)}()+"-"+l+"-"+a());s={token:n,$os:(u=navigator.userAgent,/Windows/i.test(u)?/Phone/.test(u)||/WPDesktop/.test(u)?"Windows Phone":"Windows":/(iPhone|iPad|iPod)/.test(u)?"iOS":/Android/.test(u)?"Android":/(BlackBerry|PlayBook|BB10)/i.test(u)?"BlackBerry":/Mac/i.test(u)?"Mac OS X":/Linux/.test(u)?"Linux":/CrOS/.test(u)?"Chrome OS":""),$browser:m(),$browser_version:function(){var e=m(),t=navigator.userAgent,n={"Internet Explorer Mobile":/rv:(\d+(\.\d+)?)/,"Microsoft Edge":/Edge\/(\d+(\.\d+)?)/,Chrome:/Chrome\/(\d+(\.\d+)?)/,"Chrome iOS":/CriOS\/(\d+(\.\d+)?)/,"UC Browser":/(UCBrowser|UCWEB)\/(\d+(\.\d+)?)/,Safari:/Version\/(\d+(\.\d+)?)/,"Mobile Safari":/Version\/(\d+(\.\d+)?)/,Opera:/(Opera|OPR)\/(\d+(\.\d+)?)/,Firefox:/Firefox\/(\d+(\.\d+)?)/,"Firefox iOS":/FxiOS\/(\d+(\.\d+)?)/,Konqueror:/Konqueror:(\d+(\.\d+)?)/,BlackBerry:/BlackBerry (\d+(\.\d+)?)/,"Android Mobile":/android\s(\d+(\.\d+)?)/,"Internet Explorer":/(rv:|MSIE )(\d+(\.\d+)?)/,Mozilla:/rv:(\d+(\.\d+)?)/}[e];if(void 0===n)return null;var i=t.match(n);return i?parseFloat(i[i.length-2]):null}(),$device:function(){var e=navigator.userAgent;return/Windows Phone/i.test(e)||/WPDesktop/.test(e)?"Windows Phone":/iPad/.test(e)?"iPad":/iPod/.test(e)?"iPod Touch":/iPhone/.test(e)?"iPhone":/(BlackBerry|PlayBook|BB10)/i.test(e)?"BlackBerry":/Android/.test(e)?"Android":""}(),$screen_height:screen.height,$screen_width:screen.width,$referrer:t.referrer,$referring_domain:(d=String(t.referrer||"").split("/"),d.length>=3?d[2]:""),distinct_id:f,$device_id:f,mp_lib:"mixpanel-lite",$lib_version:"1.3.0"},0===String(e.location.protocol).indexOf("http")&&(s.$current_url=e.location.href),e.device&&(e.device.manufacturer&&(s.$manufacturer=e.device.manufacturer),e.device.model&&(s.$model=e.device.model),e.device.version&&(s.$os_version=e.device.version)),s.connectionType=p();var g=navigator.connection||navigator.mozConnection||navigator.webkitConnection;g&&g.addEventListener&&g.addEventListener("change",function(){s.connectionType=p()}),r&&console.log("mixpanel.init('"+o+"')")}else console.warn("mixpanel.init: invalid token")}}(this,document);