@segment/analytics-next
Version:
Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.
181 lines • 6.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.envEnrichment = exports.ampId = exports.utm = void 0;
var tslib_1 = require("tslib");
var js_cookie_1 = tslib_1.__importDefault(require("js-cookie"));
var version_1 = require("../../generated/version");
var version_type_1 = require("../../lib/version-type");
var tld_1 = require("../../core/user/tld");
var gracefulDecodeURIComponent_1 = require("../../core/query-string/gracefulDecodeURIComponent");
var storage_1 = require("../../core/storage");
var client_hints_1 = require("../../lib/client-hints");
var cookieOptions;
function getCookieOptions() {
if (cookieOptions) {
return cookieOptions;
}
var domain = (0, tld_1.tld)(window.location.href);
cookieOptions = {
expires: 31536000000,
secure: false,
path: '/',
};
if (domain) {
cookieOptions.domain = domain;
}
return cookieOptions;
}
function ads(query) {
var queryIds = {
btid: 'dataxu',
urid: 'millennial-media',
};
if (query.startsWith('?')) {
query = query.substring(1);
}
query = query.replace(/\?/g, '&');
var parts = query.split('&');
for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
var part = parts_1[_i];
var _a = part.split('='), k = _a[0], v = _a[1];
if (queryIds[k]) {
return {
id: v,
type: queryIds[k],
};
}
}
}
function utm(query) {
if (query.startsWith('?')) {
query = query.substring(1);
}
query = query.replace(/\?/g, '&');
return query.split('&').reduce(function (acc, str) {
var _a = str.split('='), k = _a[0], _b = _a[1], v = _b === void 0 ? '' : _b;
if (k.includes('utm_') && k.length > 4) {
var utmParam = k.slice(4);
if (utmParam === 'campaign') {
utmParam = 'name';
}
acc[utmParam] = (0, gracefulDecodeURIComponent_1.gracefulDecodeURIComponent)(v);
}
return acc;
}, {});
}
exports.utm = utm;
function ampId() {
var ampId = js_cookie_1.default.get('_ga');
if (ampId && ampId.startsWith('amp')) {
return ampId;
}
}
exports.ampId = ampId;
function referrerId(query, ctx, disablePersistance) {
var _a;
var storage = new storage_1.UniversalStorage(disablePersistance ? [] : [new storage_1.CookieStorage(getCookieOptions())]);
var stored = storage.get('s:context.referrer');
var ad = (_a = ads(query)) !== null && _a !== void 0 ? _a : stored;
if (!ad) {
return;
}
if (ctx) {
ctx.referrer = tslib_1.__assign(tslib_1.__assign({}, ctx.referrer), ad);
}
storage.set('s:context.referrer', ad);
}
/**
*
* @param obj e.g. { foo: 'b', bar: 'd', baz: ['123', '456']}
* @returns e.g. 'foo=b&bar=d&baz=123&baz=456'
*/
var objectToQueryString = function (obj) {
try {
var searchParams_1 = new URLSearchParams();
Object.entries(obj).forEach(function (_a) {
var k = _a[0], v = _a[1];
if (Array.isArray(v)) {
v.forEach(function (value) { return searchParams_1.append(k, value); });
}
else {
searchParams_1.append(k, v);
}
});
return searchParams_1.toString();
}
catch (_a) {
return '';
}
};
var EnvironmentEnrichmentPlugin = /** @class */ (function () {
function EnvironmentEnrichmentPlugin() {
var _this = this;
this.name = 'Page Enrichment';
this.type = 'before';
this.version = '0.1.0';
this.isLoaded = function () { return true; };
this.load = function (_ctx, instance) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a, _1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
this.instance = instance;
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
_a = this;
return [4 /*yield*/, (0, client_hints_1.clientHints)(this.instance.options.highEntropyValuesClientHints)];
case 2:
_a.userAgentData = _b.sent();
return [3 /*break*/, 4];
case 3:
_1 = _b.sent();
return [3 /*break*/, 4];
case 4: return [2 /*return*/, Promise.resolve()];
}
});
}); };
this.enrich = function (ctx) {
var _a, _b;
// Note: Types are off - context should never be undefined here, since it is set as part of event creation.
var evtCtx = ctx.event.context;
var search = evtCtx.page.search || '';
var query = typeof search === 'object' ? objectToQueryString(search) : search;
evtCtx.userAgent = navigator.userAgent;
evtCtx.userAgentData = _this.userAgentData;
// @ts-ignore
var locale = navigator.userLanguage || navigator.language;
if (typeof evtCtx.locale === 'undefined' && typeof locale !== 'undefined') {
evtCtx.locale = locale;
}
(_a = evtCtx.library) !== null && _a !== void 0 ? _a : (evtCtx.library = {
name: 'analytics.js',
version: "".concat((0, version_type_1.getVersionType)() === 'web' ? 'next' : 'npm:next', "-").concat(version_1.version),
});
if (query && !evtCtx.campaign) {
evtCtx.campaign = utm(query);
}
var amp = ampId();
if (amp) {
evtCtx.amp = { id: amp };
}
referrerId(query, evtCtx, (_b = _this.instance.options.disableClientPersistence) !== null && _b !== void 0 ? _b : false);
try {
evtCtx.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
}
catch (_) {
// If browser doesn't have support leave timezone undefined
}
return ctx;
};
this.track = this.enrich;
this.identify = this.enrich;
this.page = this.enrich;
this.group = this.enrich;
this.alias = this.enrich;
this.screen = this.enrich;
}
return EnvironmentEnrichmentPlugin;
}());
exports.envEnrichment = new EnvironmentEnrichmentPlugin();
//# sourceMappingURL=index.js.map