class Tracking { constructor() { this.host = ''; this.protocol = 'http'; this.client = ''; this.token = ''; this.campaign = ''; this.lead = ''; this.event = ''; this.params = []; this.config = {}; this.init(); this.autoStart = true; } init(config = {}) { this.params = (new URL(document.location)).searchParams; this.setConfig(config); this.setHost(); this.setProtocol(); this.setCampaign(); this.setLead(); this.setEvent(); this.setClient(); this.setToken(); this.setAutoStart(); } setConfig(config) { if (Object.keys(config).length === 0) { let weTracking = document.getElementById("we-tracking"); if (weTracking) { config = weTracking.getAttribute("data-config"); this.config = JSON.parse(config); } } else { this.config = config; } } setAutoStart() { let start = this.params.get("s"); if (start !== "0" || (this.config.hasOwnProperty('init') && this.config.init !== "0")) { this.tracking(); } } setToken() { this.token = this.config.hasOwnProperty('token') ? this.config.token : this.getToken(); } setEvent() { this.event = this.params.get("wee"); } setLead() { this.lead = this.params.get("wel"); } setCampaign() { this.campaign = this.params.get("wec"); } setHost() { this.host = this.config.hasOwnProperty('host') ? this.config.host : 'activities.we-conect.com'; } setProtocol() { this.protocol = this.config.hasOwnProperty('protocol') ? this.config.protocol : 'https'; } setClient() { this.client = this.config.hasOwnProperty('client') ? this.config.client : 'test'; } tracking() { if (this.token === '') { return this.refreshToken(); } if (this.campaign && this.lead) { this.trackCampaign(); } } getUrl() { let url = document.location.href; url = this.event.length > 0 ? url.replace('&wee=' + this.event, '').replace('wee=' + this.event, '') : url; url = this.campaign.length > 0 ? url.replace('&wec=' + this.campaign, '').replace('wec=' + this.campaign, '') : url; url = this.lead.length > 0 ? url.replace('&wel=' + this.lead, '').replace('wel=' + this.lead, '') : url; url = url.substr(-1) === '?' ? url.slice(0, -1) : url; return url; } trackCampaign() { this.postJson(this.protocol + '://' + this.host + '/api/v1/register-action', {campaign: this.campaign, lead: this.lead, event: this.event, url: this.getUrl(), type: 'MailingClick'}, (data) => { if (data) { data = JSON.parse(data); if (data.hasOwnProperty('message') && data.message === 'Updated') { console.log('Tracking V1'); } else { console.error('Tracking issue!'); } } }); } refreshToken() { this.postJson(this.protocol + '://' + this.host + '/api/v1/login-client', {client: this.client}, (data) => { if (data) { data = JSON.parse(data); let expire = data.hasOwnProperty('expires_in') ? data.expires_in : 0; this.token = data.hasOwnProperty('token') ? data.token : ''; this.saveToken(this.token, expire); this.tracking(); } }); } postJson(url, data, success) { var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); xhr.open('POST', url); xhr.onreadystatechange = function () { if (xhr.readyState > 3 && xhr.status == 200) { success(xhr.responseText); } }; xhr.setRequestHeader('Authorization', 'bearer ' + this.token); xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.send(JSON.stringify(data)); return xhr; } getCookieTokenName() { return 'trakingToken'; } getTokenExpireDate(expire) { let d = new Date(); d.setTime(d.getTime() + (expire * 60)); return d.toUTCString(); } saveToken(token, expire) { document.cookie = this.getCookieTokenName() + "=" + token + "; expires=" + this.getTokenExpireDate(expire) + "; path=/"; } getToken() { let name = this.getCookieTokenName() + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for (var i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } } document.addEventListener("DOMContentLoaded", function () { new Tracking(); });