UNPKG

ucc-sdk

Version:
151 lines (143 loc) 4.94 kB
import axios from 'axios'; import { v4 as uuidv4 } from 'uuid'; import getBrowserFingerprint from 'get-browser-fingerprint'; let UCC_CUSTOMER_ID, UCC_PROJECT_ID, UCC_API_KEY, UCC_LANDING_PAGE_ID, UCC_SESSION_ID, UCC_SESSION_TIMESTAMP, UCC_STAGE = 'dev'; /** * Initialization Type. * @typedef {Object} Initialization * @property {string} customerId - UCC Customer ID. * @property {string} projectId - UCC Project/Source ID. * @property {string} apiKey - UCC Customer Api Key. * @property {string} pageId - UCC Landing Page Object Id. * @property {string} stage - UCC Stage. */ /** * Initialize UCC Web Tracking. * @function * @memberof track * @param {Initialization} initialization - initialization. * @returns {boolean} Web Tracking Init Response */ function initialize({ customerId, projectId, apiKey = 'live_soon_to_be_deprecated', pageId, stage = 'dev' }) { if (!customerId) { throw Error('Please provide a customerId'); } if (!apiKey) { throw Error('Please provide an apiKey'); } if (!stage) { throw Error('Please provide a stage'); } // Initialize Global Variables UCC_CUSTOMER_ID = customerId; UCC_PROJECT_ID = projectId; UCC_API_KEY = apiKey; UCC_LANDING_PAGE_ID = pageId; UCC_STAGE = stage; UCC_SESSION_ID = uuidv4(); UCC_SESSION_TIMESTAMP = Date.now(); // Return return true; } /** * Tracking Event Type. * @typedef {Object} TrackingEvent * @property {string} customerId - UCC Customer ID. * @property {string} projectId - UCC Project/Source ID. * @property {string} event - Event Name - Find out from Project Manager. * @property {object} data - Event Data - Find out from Project Manager. * @property {object} user - User Data If Applicable. * @property {string} type - Event Type If Applicable. Defaults to track */ /** * Send UCC Web Tracking Event. * @function * @memberof track * @param {TrackingEvent} trackingEvent - trackingEvent. * @returns {Promise<any>} Web Tracking Response */ async function submitEvent({ customerId, projectId, event, data, user = {}, type = 'track' }) { // Check If Event Name Is Valid if (!event) throw new Error('Event Name Is Required!'); // Proceed With Event Submission const URLS = { "dev": "https://9lsmzfho83.execute-api.eu-central-1.amazonaws.com/dev/web-tracker", "prod": "https://7ch7pvny4g.execute-api.eu-central-1.amazonaws.com/prod/web-tracker", } const ENDPOINT = URLS[UCC_STAGE]; // Get Browser Context const context = {} let WINDOW_LOCATION_HOST = null; if (window) { WINDOW_LOCATION_HOST = window.location.host; const url = new URL(window.location.href); url.searchParams.forEach((value, key) => { context[key] = value; }); context.referrer = document.referrer; context.language = navigator.language; context.languages = navigator.languages; context.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; } if (type === 'page') { data.pageId = UCC_LANDING_PAGE_ID; } else { context.pageId = UCC_LANDING_PAGE_ID; } // Returns the ISO week of the date. Date.prototype.getWeek = function () { var date = new Date(this.getTime()); date.setHours(0, 0, 0, 0); // Thursday in current week decides the year. date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7); // January 4 is always in week 1. var week1 = new Date(date.getFullYear(), 0, 4); // Adjust to Thursday in week 1 and count number of weeks from date to week1. return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7); } // Enriched Timestamp const currentdate = new Date(); const timestamp = { "iso": currentdate.toISOString(), "unix": currentdate.valueOf(), "year": currentdate.getFullYear(), "month": currentdate.getMonth() + 1, "day": currentdate.getDate(), "hour": currentdate.getHours(), "minute": currentdate.getMinutes(), "dayOfWeek": currentdate.getDay(), "weekNumber": currentdate.getWeek() } // Get Finger Print const fingerprint = getBrowserFingerprint(); // Prepare Session const session = { "guid": UCC_SESSION_ID, "timestamp": UCC_SESSION_TIMESTAMP, "elapsedTime": currentdate.valueOf() - UCC_SESSION_TIMESTAMP } // Prepare Request Body const body = { "customerId": customerId || UCC_CUSTOMER_ID, "projectId": projectId || UCC_PROJECT_ID || WINDOW_LOCATION_HOST, "event": event, "type": type, "context": { ...context, session }, "properties": data, "user": { fingerprint, ...user }, "timestamp": timestamp } const response = await axios.post(ENDPOINT, body); return response.data; } export const _submitEvent = submitEvent; export const _initialize = initialize;