UNPKG

@arcblock/analytics-js

Version:

Analytics Javascript SDK for API Service by ArcBlock

49 lines (41 loc) 1.13 kB
'use strict'; const BaseSDK = require('./base'); const Cookie = require('js-cookie'); const COOKIE_DEVICE_ID = 'abt_did'; class AnalyticsBrowserSDK extends BaseSDK { static initialize(source) { this.client = new AnalyticsBrowserSDK(source); } _getDeviceId() { let deviceId = Cookie.get(COOKIE_DEVICE_ID); if (deviceId) { return deviceId; } deviceId = this._genUUID(); Cookie.set(COOKIE_DEVICE_ID, deviceId, this._getCookieOptions()); return deviceId; } _getCookieOptions() { const options = { expires: 365, path: '/' }; const { host } = window.location; // eslint-disable-line const parts = host.split('.'); if (parts.length === 1) { options.domain = ''; } else { // Remember the cookie on all sub domains. if (parts.length > 2) { parts.shift(); } options.domain = '.' + parts.join('.'); } return options; } _getEventAttrs() { return { deviceId: this._getDeviceId(), clientTimestamp: new Date().toISOString() }; } } module.exports = AnalyticsBrowserSDK; //# sourceMappingURL=browser.js.map