@mira-network/mira-data-stream
Version:
JavaScript SDK for Mira Data Stream analytics and event tracking
2 lines (1 loc) • 5.64 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).MiraDataStream={})}(this,(function(e){"use strict";var t=function(){return t=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},t.apply(this,arguments)};function n(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,s)}a((r=r.apply(e,t||[])).next())}))}function r(e,t){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=s(0),u.throw=s(1),u.return=s(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function s(s){return function(a){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,s[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=t.call(e,i)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}function o(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var i=function(){function e(e){void 0===e&&(e={}),this.apiUrl=e.apiUrl||"http://localhost:8000/api/v1",this.userId=e.userId||null,this.anonymousId=e.anonymousId||this.generateAnonymousId(),this.writeKey=e.writeKey||"",this.queue=[],this.flushInterval=e.flushInterval||1e4,this.flushIntervalId=null,this.startFlushInterval(),console.log("MiraAnalytics initialized")}return e.prototype.startFlushInterval=function(){var e=this;"undefined"!=typeof window&&(this.flushIntervalId=setInterval((function(){return e.flush()}),this.flushInterval))},e.prototype.stopFlushInterval=function(){null!==this.flushIntervalId&&(clearInterval(this.flushIntervalId),this.flushIntervalId=null)},e.prototype.generateAnonymousId=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}))},e.prototype.identify=function(e,t){return void 0===t&&(t={}),this.userId=e,this.track("identify",t),this},e.prototype.setWriteKey=function(e){return e?(this.writeKey=e,this):(console.error("Invalid write key provided"),this)},e.prototype.track=function(e,t,n){void 0===t&&(t={});var r={event_name:e,properties:t,user_id:this.userId,anonymous_id:this.anonymousId,timestamp:(new Date).toISOString()};return console.log("Tracking event ".concat(e)),this.queue.push(r),this.queue.length>=10&&this.flush(),this},e.prototype.page=function(e,n,r){void 0===e&&(e=null),void 0===n&&(n={}),void 0===r&&(r={});var o=t(t({},n),{name:e,path:"undefined"!=typeof window?window.location.pathname:void 0,url:"undefined"!=typeof window?window.location.href:void 0,title:"undefined"!=typeof document?document.title:void 0,referrer:"undefined"!=typeof document?document.referrer:void 0});return this.track("page",o,r)},e.prototype.flush=function(){return n(this,void 0,void 0,(function(){var e,t,n,i,u;return r(this,(function(r){switch(r.label){case 0:if(0===this.queue.length)return[2];e=o([],this.queue,!0),this.queue=[],r.label=1;case 1:r.trys.push([1,6,,7]),t=0,n=e,r.label=2;case 2:return t<n.length?(i=n[t],console.log("Sending event to ".concat(this.apiUrl,"/track:"),i),"undefined"==typeof fetch?(console.warn("Fetch API not available in this environment"),[2]):this.writeKey?[4,fetch("".concat(this.apiUrl,"/track"),{method:"POST",headers:{"Content-Type":"application/json","Write-Key":this.writeKey},body:JSON.stringify(i)})]:(console.error("Write key is not set. Cannot send events to the server."),this.queue=o(o([],e,!0),this.queue,!0),[2])):[3,5];case 3:r.sent(),r.label=4;case 4:return t++,[3,2];case 5:return[3,7];case 6:return u=r.sent(),console.error("Error sending events:",u),this.queue=o(o([],e,!0),this.queue,!0),[3,7];case 7:return[2]}}))}))},e.prototype.reset=function(){return this.userId=null,this.anonymousId=this.generateAnonymousId(),this},e.prototype.dispose=function(){this.stopFlushInterval(),this.flush().catch((function(e){return console.error("Error flushing events during dispose:",e)}))},e}(),u=new i;e.MiraAnalytics=i,e.flush=function(){return u.flush()},e.identify=function(e,t){return void 0===t&&(t={}),u.identify(e,t)},e.init=function(e){return void 0===e&&(e={}),new i(e)},e.page=function(e,t,n){return void 0===e&&(e=null),void 0===t&&(t={}),void 0===n&&(n={}),u.page(e,t,n)},e.reset=function(){return u.reset()},e.setWriteKey=function(e){return u.setWriteKey(e)},e.track=function(e,t,n){return void 0===t&&(t={}),void 0===n&&(n={}),u.track(e,t,n)},Object.defineProperty(e,"__esModule",{value:!0})}));