var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
import { getQueryParam, convertToURL, maskQueryParams } from './request-utils';
import { isNull } from './type-utils';
import Config from '../config';
import { each, extend, extendArray, stripEmptyProperties } from './index';
import { document, location, userAgent, window } from './globals';
import { detectBrowser, detectBrowserVersion, detectDevice, detectDeviceType, detectOS } from './user-agent-utils';
import { stripLeadingDollar } from './string-utils';
var URL_REGEX_PREFIX = 'https?://(.*)';
// CAMPAIGN_PARAMS and EVENT_TO_PERSON_PROPERTIES should be kept in sync with
// https://github.com/PostHog/posthog/blob/master/plugin-server/src/utils/db/utils.ts#L60
// The list of campaign parameters that could be considered personal data under e.g. GDPR.
// These can be masked in URLs and properties before being sent to posthog.
export var PERSONAL_DATA_CAMPAIGN_PARAMS = [
'gclid', // google ads
'gclsrc', // google ads 360
'dclid', // google display ads
'gbraid', // google ads, web to app
'wbraid', // google ads, app to web
'fbclid', // facebook
'msclkid', // microsoft
'twclid', // twitter
'li_fat_id', // linkedin
'igshid', // instagram
'ttclid', // tiktok
'rdt_cid', // reddit
'irclid', // impact
'_kx', // klaviyo
];
export var CAMPAIGN_PARAMS = extendArray([
'utm_source',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_term',
'gad_source', // google ads source
'mc_cid', // mailchimp campaign id
], PERSONAL_DATA_CAMPAIGN_PARAMS);
export var EVENT_TO_PERSON_PROPERTIES = [
// mobile params
'$app_build',
'$app_name',
'$app_namespace',
'$app_version',
// web params
'$browser',
'$browser_version',
'$device_type',
'$current_url',
'$pathname',
'$os',
'$os_name', // $os_name is a special case, it's treated as an alias of $os!
'$os_version',
'$referring_domain',
'$referrer',
'$screen_height',
'$screen_width',
'$viewport_height',
'$viewport_width',
'$raw_user_agent',
];
export var MASKED = '<masked>';
export var Info = {
campaignParams: function (_a) {
var _b = _a === void 0 ? {} : _a, customTrackedParams = _b.customTrackedParams, maskPersonalDataProperties = _b.maskPersonalDataProperties, customPersonalDataProperties = _b.customPersonalDataProperties;
if (!document) {
return {};
}
var paramsToMask = maskPersonalDataProperties
? extendArray([], PERSONAL_DATA_CAMPAIGN_PARAMS, customPersonalDataProperties || [])
: [];
return this._campaignParamsFromUrl(maskQueryParams(document.URL, paramsToMask, MASKED), customTrackedParams);
},
_campaignParamsFromUrl: function (url, customParams) {
var campaign_keywords = CAMPAIGN_PARAMS.concat(customParams || []);
var params = {};
each(campaign_keywords, function (kwkey) {
var kw = getQueryParam(url, kwkey);
params[kwkey] = kw ? kw : null;
});
return params;
},
_searchEngine: function (referrer) {
if (!referrer) {
return null;
}
else {
if (referrer.search(URL_REGEX_PREFIX + 'google.([^/?]*)') === 0) {
return 'google';
}
else if (referrer.search(URL_REGEX_PREFIX + 'bing.com') === 0) {
return 'bing';
}
else if (referrer.search(URL_REGEX_PREFIX + 'yahoo.com') === 0) {
return 'yahoo';
}
else if (referrer.search(URL_REGEX_PREFIX + 'duckduckgo.com') === 0) {
return 'duckduckgo';
}
else {
return null;
}
}
},
_searchInfoFromReferrer: function (referrer) {
var search = Info._searchEngine(referrer);
var param = search != 'yahoo' ? 'q' : 'p';
var ret = {};
if (!isNull(search)) {
ret['$search_engine'] = search;
var keyword = document ? getQueryParam(document.referrer, param) : '';
if (keyword.length) {
ret['ph_keyword'] = keyword;
}
}
return ret;
},
searchInfo: function () {
var referrer = document === null || document === void 0 ? void 0 : document.referrer;
if (!referrer) {
return {};
}
return this._searchInfoFromReferrer(referrer);
},
/**
* This function detects which browser is running this script.
* The order of the checks are important since many user agents
* include keywords used in later checks.
*/
browser: detectBrowser,
/**
* This function detects which browser version is running this script,
* parsing major and minor version (e.g., 42.1). User agent strings from:
* http://www.useragentstring.com/pages/useragentstring.php
*
* `navigator.vendor` is passed in and used to help with detecting certain browsers
* NB `navigator.vendor` is deprecated and not present in every browser
*/
browserVersion: detectBrowserVersion,
browserLanguage: function () {
return (navigator.language || // Any modern browser
navigator.userLanguage // IE11
);
},
browserLanguagePrefix: function () {
var browserLanguage = this.browserLanguage();
return typeof browserLanguage === 'string' ? browserLanguage.split('-')[0] : undefined;
},
os: detectOS,
device: detectDevice,
deviceType: detectDeviceType,
referrer: function () {
return (document === null || document === void 0 ? void 0 : document.referrer) || '$direct';
},
referringDomain: function () {
var _a;
if (!(document === null || document === void 0 ? void 0 : document.referrer)) {
return '$direct';
}
return ((_a = convertToURL(document.referrer)) === null || _a === void 0 ? void 0 : _a.host) || '$direct';
},
referrerInfo: function () {
return {
$referrer: this.referrer(),
$referring_domain: this.referringDomain(),
};
},
personInfo: function (_a) {
var _b = _a === void 0 ? {} : _a, maskPersonalDataProperties = _b.maskPersonalDataProperties, customPersonalDataProperties = _b.customPersonalDataProperties;
var paramsToMask = maskPersonalDataProperties
? extendArray([], PERSONAL_DATA_CAMPAIGN_PARAMS, customPersonalDataProperties || [])
: [];
var url = location === null || location === void 0 ? void 0 : location.href.substring(0, 1000);
// we're being a bit more economical with bytes here because this is stored in the cookie
return {
r: this.referrer().substring(0, 1000),
u: url ? maskQueryParams(url, paramsToMask, MASKED) : undefined,
};
},
personPropsFromInfo: function (info) {
var _a;
var referrer = info.r, url = info.u;
var referring_domain = referrer == null ? undefined : referrer == '$direct' ? '$direct' : (_a = convertToURL(referrer)) === null || _a === void 0 ? void 0 : _a.host;
var props = {
$referrer: referrer,
$referring_domain: referring_domain,
};
if (url) {
props['$current_url'] = url;
var location_1 = convertToURL(url);
props['$host'] = location_1 === null || location_1 === void 0 ? void 0 : location_1.host;
props['$pathname'] = location_1 === null || location_1 === void 0 ? void 0 : location_1.pathname;
var campaignParams = this._campaignParamsFromUrl(url);
extend(props, campaignParams);
}
if (referrer) {
var searchInfo = this._searchInfoFromReferrer(referrer);
extend(props, searchInfo);
}
return props;
},
initialPersonPropsFromInfo: function (info) {
var personProps = this.personPropsFromInfo(info);
var props = {};
each(personProps, function (val, key) {
props["$initial_".concat(stripLeadingDollar(key))] = val;
});
return props;
},
timezone: function () {
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
catch (_a) {
return undefined;
}
},
timezoneOffset: function () {
try {
return new Date().getTimezoneOffset();
}
catch (_a) {
return undefined;
}
},
properties: function (_a) {
var _b = _a === void 0 ? {} : _a, maskPersonalDataProperties = _b.maskPersonalDataProperties, customPersonalDataProperties = _b.customPersonalDataProperties;
if (!userAgent) {
return {};
}
var paramsToMask = maskPersonalDataProperties
? extendArray([], PERSONAL_DATA_CAMPAIGN_PARAMS, customPersonalDataProperties || [])
: [];
var _c = __read(Info.os(userAgent), 2), os_name = _c[0], os_version = _c[1];
return extend(stripEmptyProperties({
$os: os_name,
$os_version: os_version,
$browser: Info.browser(userAgent, navigator.vendor),
$device: Info.device(userAgent),
$device_type: Info.deviceType(userAgent),
$timezone: Info.timezone(),
$timezone_offset: Info.timezoneOffset(),
}), {
$current_url: maskQueryParams(location === null || location === void 0 ? void 0 : location.href, paramsToMask, MASKED),
$host: location === null || location === void 0 ? void 0 : location.host,
$pathname: location === null || location === void 0 ? void 0 : location.pathname,
$raw_user_agent: userAgent.length > 1000 ? userAgent.substring(0, 997) + '...' : userAgent,
$browser_version: Info.browserVersion(userAgent, navigator.vendor),
$browser_language: Info.browserLanguage(),
$browser_language_prefix: Info.browserLanguagePrefix(),
$screen_height: window === null || window === void 0 ? void 0 : window.screen.height,
$screen_width: window === null || window === void 0 ? void 0 : window.screen.width,
$viewport_height: window === null || window === void 0 ? void 0 : window.innerHeight,
$viewport_width: window === null || window === void 0 ? void 0 : window.innerWidth,
$lib: 'web',
$lib_version: Config.LIB_VERSION,
$insert_id: Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10),
$time: Date.now() / 1000, // epoch time in seconds
});
},
people_properties: function () {
if (!userAgent) {
return {};
}
var _a = __read(Info.os(userAgent), 2), os_name = _a[0], os_version = _a[1];
return extend(stripEmptyProperties({
$os: os_name,
$os_version: os_version,
$browser: Info.browser(userAgent, navigator.vendor),
}), {
$browser_version: Info.browserVersion(userAgent, navigator.vendor),
});
},
};
//# sourceMappingURL=event-utils.js.map