UNPKG

@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.

25 lines 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addPageContext = void 0; var tslib_1 = require("tslib"); var pick_1 = require("../../lib/pick"); var analytics_page_tools_1 = require("@segment/analytics-page-tools"); /** * Augments a segment event with information about the current page. * Page information like URL changes frequently, so this is meant to be captured as close to the event call as possible. * Things like `userAgent` do not change, so they can be added later in the flow. * We prefer not to add this information to this function, as it increases the main bundle size. */ var addPageContext = function (event, pageCtx) { if (pageCtx === void 0) { pageCtx = (0, analytics_page_tools_1.getDefaultPageContext)(); } var evtCtx = event.context; // Context should be set earlier in the flow var pageContextFromEventProps; if (event.type === 'page') { pageContextFromEventProps = event.properties && (0, pick_1.pick)(event.properties, Object.keys(pageCtx)); event.properties = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, pageCtx), event.properties), (event.name ? { name: event.name } : {})); } evtCtx.page = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, pageCtx), pageContextFromEventProps), evtCtx.page); }; exports.addPageContext = addPageContext; //# sourceMappingURL=add-page-context.js.map