UNPKG

moengage

Version:

Moengage events integration for web

105 lines (91 loc) 5.39 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } // import React from 'react'; // import './style.css'; var Events = function () { function Events(data) { _classCallCheck(this, Events); // console.log(data); var appID = data.appID, debugMode = data.debugMode, swPath = data.swPath; var doc = (typeof document === 'undefined' ? 'undefined' : _typeof(document)) !== undefined ? document : {}; if (doc) { var script_tag = doc.createElement('script'); script_tag.type = 'text/javascript'; script_tag.text = '(function(i,s,o,g,r,a,m,n){i.moengage_object=r;t={};q=function(f){return function(){(i.moengage_q=i.moengage_q||[]).push({f:f,a:arguments})}};f=[\'track_event\',\'add_user_attribute\',\'add_first_name\',\'add_last_name\',\'add_email\',\'add_mobile\',\'add_user_name\',\'add_gender\',\'add_birthday\',\'destroy_session\',\'add_unique_user_id\',\'moe_events\',\'call_web_push\',\'track\',\'location_type_attribute\'],h={onsite:["getData","registerCallback"]};for(k in f){t[f[k]]=q(f[k])}for(k in h)for(l in h[k]){null==t[k]&&(t[k]={}),t[k][h[k][l]]=q(k+"."+h[k][l])}a=s.createElement(o);m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);i.moe=i.moe||function(){n=arguments[0];return t};a.onload=function(){if(n){i[r]=moe(n)}}})(window,document,\'script\',\'https://cdn.moengage.com/webpush/moe_webSdk.min.latest.js\',\'Moengage\')\n Moengage = moe({\n app_id:\'' + appID + '\',\n debug_logs: ' + (debugMode ? debugMode : 0) + ',\n swPath: \'' + (swPath ? swPath : "/service-worker.js") + '\'\n });'; doc.head.appendChild(script_tag); } this.checkMo = this.checkMo.bind(this); this.updateUniqueUser = this.updateUniqueUser.bind(this); } _createClass(Events, [{ key: 'checkMo', value: function checkMo() { try { var Moengag = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== undefined && window.Moengage; if (Moengag) return Moengag; } catch (error) { console.error("Moengage not found or initialise properly"); console.log(error); } return false; } }, { key: 'addEvent', value: function addEvent(eventName, attrObj) { this.checkMo() && this.checkMo().track_event(eventName, attrObj); } }, { key: 'setCustomAttribute', value: function setCustomAttribute(key, value) { this.checkMo() && this.checkMo().add_user_attribute(key, value); } }, { key: 'updateAttributes', value: function updateAttributes(_ref) { var name = _ref.name, email = _ref.email, mobile = _ref.mobile, addFirstName = _ref.addFirstName, addLastName = _ref.addLastName, addGender = _ref.addGender, addBirthday = _ref.addBirthday; if (this.checkMo()) { email && this.checkMo().add_email(email); mobile && this.checkMo().add_mobile(mobile); name && this.checkMo().add_user_name(name); addFirstName && this.checkMo().add_first_name(addFirstName); addLastName && this.checkMo().add_last_name(addLastName); addGender && this.checkMo().add_gender(addGender); addBirthday && this.checkMo().add_birthday(addBirthday); } } }, { key: 'createUniqueUser', value: function createUniqueUser(UNIQUE_ID) { this.checkMo() && this.checkMo().add_unique_user_id(UNIQUE_ID); } }, { key: 'updateUniqueUser', value: function updateUniqueUser(NEW_UNIQUE_ID) { if (this.checkMo() && this.checkMo().update_unique_user_id) { this.checkMo().update_unique_user_id(NEW_UNIQUE_ID); } else { this.createUniqueUser(NEW_UNIQUE_ID); } } }, { key: 'destroySession', value: function destroySession() { this.checkMo() && this.checkMo().destroy_session(); } }]); return Events; }(); exports.default = Events;