@deriv-com/analytics 1.36.0 → 1.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +853 -72
- package/dist/browser/analytics.bundle.global.js +23 -0
- package/dist/browser/analytics.bundle.global.js.map +1 -0
- package/dist/browser/analytics.esm.mjs +9 -0
- package/dist/browser/analytics.esm.mjs.map +1 -0
- package/dist/chunk-3LFZFQL4.mjs +3 -0
- package/dist/chunk-3LFZFQL4.mjs.map +1 -0
- package/dist/chunk-DNCZM4KN.mjs +4 -0
- package/dist/chunk-DNCZM4KN.mjs.map +1 -0
- package/dist/chunk-EMF3RT6E.mjs +14 -0
- package/dist/chunk-EMF3RT6E.mjs.map +1 -0
- package/dist/chunk-HQX3Z7PF.mjs +3 -0
- package/dist/chunk-HQX3Z7PF.mjs.map +1 -0
- package/dist/chunk-KDJ46GSX.mjs +3 -0
- package/dist/chunk-KDJ46GSX.mjs.map +1 -0
- package/dist/chunk-LKLVBL24.js +3 -0
- package/dist/chunk-LKLVBL24.js.map +1 -0
- package/dist/chunk-NOEKD4DT.js +4 -0
- package/dist/chunk-NOEKD4DT.js.map +1 -0
- package/dist/chunk-NZ35MU4O.js +3 -0
- package/dist/chunk-NZ35MU4O.js.map +1 -0
- package/dist/growthbook-NJGOOJH4.mjs +3 -0
- package/dist/growthbook-NJGOOJH4.mjs.map +1 -0
- package/dist/index.d.mts +114 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/dist/metafile-cjs.json +1 -0
- package/dist/metafile-esm.json +1 -0
- package/dist/metafile-iife.json +1 -0
- package/dist/posthog-D3QL9y8a.d.ts +95 -0
- package/dist/posthog-DGiwrEsA.d.mts +95 -0
- package/dist/posthog-P3AXUMLM.mjs +6 -0
- package/dist/posthog-P3AXUMLM.mjs.map +1 -0
- package/dist/providers/growthbook/index.d.mts +73 -0
- package/dist/providers/growthbook/index.d.ts +73 -0
- package/dist/providers/growthbook/index.js +3 -0
- package/dist/providers/growthbook/index.js.map +1 -0
- package/dist/providers/growthbook/index.mjs +3 -0
- package/dist/providers/growthbook/index.mjs.map +1 -0
- package/dist/providers/posthog/index.d.mts +3 -0
- package/dist/providers/posthog/index.d.ts +3 -0
- package/dist/providers/posthog/index.js +3 -0
- package/dist/providers/posthog/index.js.map +1 -0
- package/dist/providers/posthog/index.mjs +3 -0
- package/dist/providers/posthog/index.mjs.map +1 -0
- package/dist/providers/rudderstack/index.d.mts +73 -0
- package/dist/providers/rudderstack/index.d.ts +73 -0
- package/dist/providers/rudderstack/index.js +2 -0
- package/dist/providers/rudderstack/index.js.map +1 -0
- package/dist/providers/rudderstack/index.mjs +2 -0
- package/dist/providers/rudderstack/index.mjs.map +1 -0
- package/{lib/types.d.ts → dist/types-DoMejCXv.d.mts} +18 -29
- package/dist/types-DoMejCXv.d.ts +361 -0
- package/dist/utils/analytics-cache/index.d.mts +138 -0
- package/dist/utils/analytics-cache/index.d.ts +138 -0
- package/dist/utils/analytics-cache/index.js +2 -0
- package/dist/utils/analytics-cache/index.js.map +1 -0
- package/dist/utils/analytics-cache/index.mjs +2 -0
- package/dist/utils/analytics-cache/index.mjs.map +1 -0
- package/package.json +97 -68
- package/dist/analytics.bundle.js +0 -1
- package/lib/analytics.d.ts +0 -66
- package/lib/analytics.js +0 -418
- package/lib/growthbook.d.ts +0 -29
- package/lib/growthbook.js +0 -144
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -5
- package/lib/posthog.d.ts +0 -28
- package/lib/posthog.js +0 -154
- package/lib/rudderstack.d.ts +0 -23
- package/lib/rudderstack.js +0 -100
- package/lib/types.js +0 -2
package/lib/analytics.d.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Growthbook, GrowthbookConfigs } from './growthbook';
|
|
2
|
-
import { RudderStack } from './rudderstack';
|
|
3
|
-
import { PostHogAnalytics, PostHogConfig } from './posthog';
|
|
4
|
-
import { TCoreAttributes, TGrowthbookOptions, TAllEvents } from './types';
|
|
5
|
-
declare global {
|
|
6
|
-
interface Window {
|
|
7
|
-
AnalyticsInstance: ReturnType<typeof createAnalyticsInstance>;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
type Options = {
|
|
11
|
-
growthbookKey?: string;
|
|
12
|
-
growthbookDecryptionKey?: string;
|
|
13
|
-
rudderstackKey?: string;
|
|
14
|
-
posthogKey?: string;
|
|
15
|
-
posthogHost?: string;
|
|
16
|
-
posthogConfig?: PostHogConfig;
|
|
17
|
-
growthbookOptions?: TGrowthbookOptions;
|
|
18
|
-
enableBotFiltering?: boolean;
|
|
19
|
-
};
|
|
20
|
-
export declare function createAnalyticsInstance(options?: Options): {
|
|
21
|
-
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, posthogKey, posthogHost, posthogConfig, growthbookOptions, enableBotFiltering, }: Options) => Promise<void>;
|
|
22
|
-
setAttributes: ({ country, user_language, device_language, device_type, account_type, user_id, anonymous_id, app_id, utm_source, utm_medium, utm_campaign, is_authorised, residence_country, url, domain, geo_location, loggedIn, network_downlink, network_rtt, network_type, account_currency, account_mode, }: TCoreAttributes) => void;
|
|
23
|
-
identifyEvent: (user_id?: string) => void;
|
|
24
|
-
getFeatureState: (id: string) => string | undefined;
|
|
25
|
-
getFeatureValue: <K extends string | number, V extends GrowthbookConfigs[K]>(id: K, defaultValue: V) => import("@growthbook/growthbook").WidenPrimitives<V>;
|
|
26
|
-
getGrowthbookStatus: () => Promise<{
|
|
27
|
-
isLoaded: boolean;
|
|
28
|
-
status: void | import("@growthbook/growthbook").InitResponse;
|
|
29
|
-
}>;
|
|
30
|
-
isFeatureOn: (key: string) => boolean;
|
|
31
|
-
setUrl: (href: string) => Promise<void>;
|
|
32
|
-
getId: () => string;
|
|
33
|
-
getAnonymousId: () => string;
|
|
34
|
-
trackEvent: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, analytics_data: TAllEvents[T]) => void;
|
|
35
|
-
getInstances: () => {
|
|
36
|
-
ab: Growthbook;
|
|
37
|
-
tracking: RudderStack;
|
|
38
|
-
posthog: PostHogAnalytics;
|
|
39
|
-
};
|
|
40
|
-
pageView: (current_page: string, platform?: string, properties?: Record<string, unknown>) => void;
|
|
41
|
-
reset: () => void;
|
|
42
|
-
};
|
|
43
|
-
export declare const Analytics: {
|
|
44
|
-
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, posthogKey, posthogHost, posthogConfig, growthbookOptions, enableBotFiltering, }: Options) => Promise<void>;
|
|
45
|
-
setAttributes: ({ country, user_language, device_language, device_type, account_type, user_id, anonymous_id, app_id, utm_source, utm_medium, utm_campaign, is_authorised, residence_country, url, domain, geo_location, loggedIn, network_downlink, network_rtt, network_type, account_currency, account_mode, }: TCoreAttributes) => void;
|
|
46
|
-
identifyEvent: (user_id?: string) => void;
|
|
47
|
-
getFeatureState: (id: string) => string | undefined;
|
|
48
|
-
getFeatureValue: <K extends string | number, V extends GrowthbookConfigs[K]>(id: K, defaultValue: V) => import("@growthbook/growthbook").WidenPrimitives<V>;
|
|
49
|
-
getGrowthbookStatus: () => Promise<{
|
|
50
|
-
isLoaded: boolean;
|
|
51
|
-
status: void | import("@growthbook/growthbook").InitResponse;
|
|
52
|
-
}>;
|
|
53
|
-
isFeatureOn: (key: string) => boolean;
|
|
54
|
-
setUrl: (href: string) => Promise<void>;
|
|
55
|
-
getId: () => string;
|
|
56
|
-
getAnonymousId: () => string;
|
|
57
|
-
trackEvent: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, analytics_data: TAllEvents[T]) => void;
|
|
58
|
-
getInstances: () => {
|
|
59
|
-
ab: Growthbook;
|
|
60
|
-
tracking: RudderStack;
|
|
61
|
-
posthog: PostHogAnalytics;
|
|
62
|
-
};
|
|
63
|
-
pageView: (current_page: string, platform?: string, properties?: Record<string, unknown>) => void;
|
|
64
|
-
reset: () => void;
|
|
65
|
-
};
|
|
66
|
-
export {};
|
package/lib/analytics.js
DELETED
|
@@ -1,418 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
-
};
|
|
52
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
exports.Analytics = exports.createAnalyticsInstance = void 0;
|
|
54
|
-
var growthbook_1 = require("./growthbook");
|
|
55
|
-
var rudderstack_1 = require("./rudderstack");
|
|
56
|
-
var posthog_1 = require("./posthog");
|
|
57
|
-
var js_cookie_1 = __importDefault(require("js-cookie"));
|
|
58
|
-
var utils_1 = require("@deriv-com/utils");
|
|
59
|
-
var CACHE_COOKIE_EVENTS = 'cached_analytics_events';
|
|
60
|
-
var CACHE_COOKIE_PAGES = 'cached_analytics_page_views';
|
|
61
|
-
var isLikelyBot = function () {
|
|
62
|
-
var _a, _b;
|
|
63
|
-
if (typeof window === 'undefined' || typeof navigator === 'undefined')
|
|
64
|
-
return false;
|
|
65
|
-
var ua = ((_b = (_a = navigator.userAgent) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.trim()) || '';
|
|
66
|
-
var botPatterns = [
|
|
67
|
-
'bot',
|
|
68
|
-
'crawler',
|
|
69
|
-
'spider',
|
|
70
|
-
'scraper',
|
|
71
|
-
'headless',
|
|
72
|
-
'phantom',
|
|
73
|
-
'selenium',
|
|
74
|
-
'puppeteer',
|
|
75
|
-
'playwright',
|
|
76
|
-
'wget',
|
|
77
|
-
'curl',
|
|
78
|
-
'python-requests',
|
|
79
|
-
'python-urllib',
|
|
80
|
-
'java/',
|
|
81
|
-
'apache-http',
|
|
82
|
-
'node-fetch',
|
|
83
|
-
'axios',
|
|
84
|
-
'googlebot',
|
|
85
|
-
'bingbot',
|
|
86
|
-
'yandex',
|
|
87
|
-
'baiduspider',
|
|
88
|
-
'facebookexternalhit',
|
|
89
|
-
'twitterbot',
|
|
90
|
-
'linkedinbot',
|
|
91
|
-
'slackbot',
|
|
92
|
-
'telegrambot',
|
|
93
|
-
'whatsapp',
|
|
94
|
-
'discordbot',
|
|
95
|
-
];
|
|
96
|
-
if (botPatterns.some(function (pattern) { return ua.includes(pattern); }))
|
|
97
|
-
return true;
|
|
98
|
-
if (navigator.webdriver === true)
|
|
99
|
-
return true;
|
|
100
|
-
if (!navigator.languages || navigator.languages.length === 0)
|
|
101
|
-
return true;
|
|
102
|
-
if (ua.includes('chrome') && !window.chrome)
|
|
103
|
-
return true;
|
|
104
|
-
return false;
|
|
105
|
-
};
|
|
106
|
-
function createAnalyticsInstance(options) {
|
|
107
|
-
var _this = this;
|
|
108
|
-
var _growthbook, _rudderstack, _posthog, _enableBotFiltering = false, core_data = {}, tracking_config = {}, offline_event_cache = [], _pending_identify_calls = [], _cookie_cache_processed = false;
|
|
109
|
-
var getAllowedDomain = function () {
|
|
110
|
-
if (typeof window === 'undefined')
|
|
111
|
-
return '.deriv.com';
|
|
112
|
-
var allowedDomains = ['deriv.com', 'deriv.team', 'deriv.ae'];
|
|
113
|
-
var hostname = window.location.hostname;
|
|
114
|
-
if (hostname === 'localhost')
|
|
115
|
-
return '';
|
|
116
|
-
var matched = allowedDomains.find(function (d) { return hostname.includes(d); });
|
|
117
|
-
return matched ? ".".concat(matched) : ".".concat(allowedDomains[0]);
|
|
118
|
-
};
|
|
119
|
-
var processCookieCache = function () {
|
|
120
|
-
if (_cookie_cache_processed)
|
|
121
|
-
return;
|
|
122
|
-
if (!(_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.has_initialized) && !(_posthog === null || _posthog === void 0 ? void 0 : _posthog.has_initialized))
|
|
123
|
-
return;
|
|
124
|
-
_cookie_cache_processed = true;
|
|
125
|
-
var domain = getAllowedDomain();
|
|
126
|
-
var cookieOptions = domain ? { domain: domain } : {};
|
|
127
|
-
try {
|
|
128
|
-
var storedEventsString = js_cookie_1.default.get(CACHE_COOKIE_EVENTS);
|
|
129
|
-
if (storedEventsString) {
|
|
130
|
-
var storedEvents = JSON.parse(storedEventsString);
|
|
131
|
-
if (Array.isArray(storedEvents) && storedEvents.length > 0) {
|
|
132
|
-
storedEvents.forEach(function (event) {
|
|
133
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event.name, event.properties);
|
|
134
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.track(event.name, event.properties);
|
|
135
|
-
});
|
|
136
|
-
js_cookie_1.default.remove(CACHE_COOKIE_EVENTS, cookieOptions);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
var storedPagesString = js_cookie_1.default.get(CACHE_COOKIE_PAGES);
|
|
140
|
-
if (storedPagesString) {
|
|
141
|
-
var storedPages = JSON.parse(storedPagesString);
|
|
142
|
-
if (Array.isArray(storedPages) && storedPages.length > 0) {
|
|
143
|
-
storedPages.forEach(function (page) {
|
|
144
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(page.name, 'Deriv App', getId(), page.properties);
|
|
145
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.pageView(page.name, 'Deriv App', getId(), page.properties);
|
|
146
|
-
});
|
|
147
|
-
js_cookie_1.default.remove(CACHE_COOKIE_PAGES, cookieOptions);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
catch (err) {
|
|
152
|
-
console.warn('Analytics: Failed to process cookie cache', err);
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
var cacheEventToCookie = function (eventName, properties) {
|
|
156
|
-
try {
|
|
157
|
-
var domain = getAllowedDomain();
|
|
158
|
-
var existingCache = js_cookie_1.default.get(CACHE_COOKIE_EVENTS);
|
|
159
|
-
var events = existingCache ? JSON.parse(existingCache) : [];
|
|
160
|
-
events.push({ name: eventName, properties: properties, timestamp: Date.now() });
|
|
161
|
-
var cookieOptions = { expires: 1 };
|
|
162
|
-
if (domain)
|
|
163
|
-
cookieOptions.domain = domain;
|
|
164
|
-
js_cookie_1.default.set(CACHE_COOKIE_EVENTS, JSON.stringify(events), cookieOptions);
|
|
165
|
-
}
|
|
166
|
-
catch (err) {
|
|
167
|
-
console.warn('Analytics: Failed to cache event', err);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
var cachePageViewToCookie = function (pageName, properties) {
|
|
171
|
-
try {
|
|
172
|
-
var domain = getAllowedDomain();
|
|
173
|
-
var existingCache = js_cookie_1.default.get(CACHE_COOKIE_PAGES);
|
|
174
|
-
var pages = existingCache ? JSON.parse(existingCache) : [];
|
|
175
|
-
pages.push({ name: pageName, properties: properties, timestamp: Date.now() });
|
|
176
|
-
var cookieOptions = { expires: 1 };
|
|
177
|
-
if (domain)
|
|
178
|
-
cookieOptions.domain = domain;
|
|
179
|
-
js_cookie_1.default.set(CACHE_COOKIE_PAGES, JSON.stringify(pages), cookieOptions);
|
|
180
|
-
}
|
|
181
|
-
catch (err) {
|
|
182
|
-
console.warn('Analytics: Failed to cache page view', err);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
var getClientCountry = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
186
|
-
var countryFromCloudflare, countryFromCookie, websiteStatus, countryFromWebsiteStatus;
|
|
187
|
-
var _a;
|
|
188
|
-
return __generator(this, function (_b) {
|
|
189
|
-
switch (_b.label) {
|
|
190
|
-
case 0: return [4 /*yield*/, utils_1.CountryUtils.getCountry()];
|
|
191
|
-
case 1:
|
|
192
|
-
countryFromCloudflare = _b.sent();
|
|
193
|
-
countryFromCookie = js_cookie_1.default.get('clients_country');
|
|
194
|
-
websiteStatus = js_cookie_1.default.get('website_status');
|
|
195
|
-
countryFromWebsiteStatus = '';
|
|
196
|
-
if (websiteStatus) {
|
|
197
|
-
try {
|
|
198
|
-
countryFromWebsiteStatus = ((_a = JSON.parse(websiteStatus)) === null || _a === void 0 ? void 0 : _a.clients_country) || '';
|
|
199
|
-
}
|
|
200
|
-
catch (err) {
|
|
201
|
-
console.warn('Analytics: Failed to parse website_status cookie', err);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return [2 /*return*/, countryFromCookie || countryFromWebsiteStatus || countryFromCloudflare];
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
}); };
|
|
208
|
-
var onSdkLoaded = function () {
|
|
209
|
-
processCookieCache();
|
|
210
|
-
_pending_identify_calls.forEach(function (userId) {
|
|
211
|
-
if (userId) {
|
|
212
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(userId, { language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en' });
|
|
213
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.identifyEvent(userId, { language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en' });
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
_pending_identify_calls = [];
|
|
217
|
-
};
|
|
218
|
-
var initialise = function (_a) {
|
|
219
|
-
var growthbookKey = _a.growthbookKey, growthbookDecryptionKey = _a.growthbookDecryptionKey, rudderstackKey = _a.rudderstackKey, posthogKey = _a.posthogKey, posthogHost = _a.posthogHost, posthogConfig = _a.posthogConfig, growthbookOptions = _a.growthbookOptions, _b = _a.enableBotFiltering, enableBotFiltering = _b === void 0 ? false : _b;
|
|
220
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
221
|
-
var country, _c, anonymousId, anonId, interval_1, err_1;
|
|
222
|
-
var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
|
|
223
|
-
var _0, _1;
|
|
224
|
-
return __generator(this, function (_2) {
|
|
225
|
-
switch (_2.label) {
|
|
226
|
-
case 0:
|
|
227
|
-
_2.trys.push([0, 3, , 4]);
|
|
228
|
-
_enableBotFiltering = enableBotFiltering;
|
|
229
|
-
_c = ((_d = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _d === void 0 ? void 0 : _d.country);
|
|
230
|
-
if (_c) return [3 /*break*/, 2];
|
|
231
|
-
return [4 /*yield*/, getClientCountry()];
|
|
232
|
-
case 1:
|
|
233
|
-
_c = (_2.sent());
|
|
234
|
-
_2.label = 2;
|
|
235
|
-
case 2:
|
|
236
|
-
country = _c;
|
|
237
|
-
if (rudderstackKey) {
|
|
238
|
-
_rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey, onSdkLoaded);
|
|
239
|
-
}
|
|
240
|
-
if (posthogKey) {
|
|
241
|
-
_posthog = posthog_1.PostHogAnalytics.getPostHogInstance(posthogKey, posthogHost || 'https://ph.deriv.com', onSdkLoaded, posthogConfig);
|
|
242
|
-
}
|
|
243
|
-
if ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && Object.keys(growthbookOptions.attributes).length > 0) {
|
|
244
|
-
anonymousId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getAnonymousId()) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.getAnonymousId());
|
|
245
|
-
core_data = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, core_data), { country: country }), (((_e = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _e === void 0 ? void 0 : _e.user_language) && {
|
|
246
|
-
user_language: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.user_language,
|
|
247
|
-
})), (((_f = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _f === void 0 ? void 0 : _f.account_type) && {
|
|
248
|
-
account_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_type,
|
|
249
|
-
})), (((_g = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _g === void 0 ? void 0 : _g.app_id) && { app_id: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.app_id })), (((_h = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _h === void 0 ? void 0 : _h.residence_country) && {
|
|
250
|
-
residence_country: (_j = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _j === void 0 ? void 0 : _j.residence_country,
|
|
251
|
-
})), (((_k = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _k === void 0 ? void 0 : _k.device_type) && {
|
|
252
|
-
device_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.device_type,
|
|
253
|
-
})), (((_l = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _l === void 0 ? void 0 : _l.url) && { url: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.url })), ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && { loggedIn: !!((_m = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _m === void 0 ? void 0 : _m.loggedIn) })), (((_o = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _o === void 0 ? void 0 : _o.email_hash) && {
|
|
254
|
-
email_hash: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.email_hash,
|
|
255
|
-
})), (((_p = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _p === void 0 ? void 0 : _p.network_type) && {
|
|
256
|
-
network_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_type,
|
|
257
|
-
})), (((_q = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _q === void 0 ? void 0 : _q.network_rtt) && {
|
|
258
|
-
network_rtt: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_rtt,
|
|
259
|
-
})), (((_r = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _r === void 0 ? void 0 : _r.network_downlink) && {
|
|
260
|
-
network_downlink: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_downlink,
|
|
261
|
-
})), (((_s = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _s === void 0 ? void 0 : _s.user_id) &&
|
|
262
|
-
!isUUID((_t = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _t === void 0 ? void 0 : _t.user_id) && {
|
|
263
|
-
user_id: (_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.user_id,
|
|
264
|
-
})), (anonymousId && { anonymous_id: anonymousId })), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_currency) && {
|
|
265
|
-
account_currency: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_currency,
|
|
266
|
-
})), (((_w = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _w === void 0 ? void 0 : _w.account_mode) && {
|
|
267
|
-
account_mode: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_mode,
|
|
268
|
-
}));
|
|
269
|
-
}
|
|
270
|
-
growthbookOptions !== null && growthbookOptions !== void 0 ? growthbookOptions : (growthbookOptions = {});
|
|
271
|
-
(_x = growthbookOptions.attributes) !== null && _x !== void 0 ? _x : (growthbookOptions.attributes = {});
|
|
272
|
-
anonId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getAnonymousId()) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.getAnonymousId());
|
|
273
|
-
(_y = (_0 = growthbookOptions.attributes).id) !== null && _y !== void 0 ? _y : (_0.id = anonId);
|
|
274
|
-
(_z = (_1 = growthbookOptions.attributes).country) !== null && _z !== void 0 ? _z : (_1.country = country);
|
|
275
|
-
if (growthbookKey) {
|
|
276
|
-
_growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions);
|
|
277
|
-
interval_1 = setInterval(function () {
|
|
278
|
-
if (Object.keys(tracking_config).length > 0)
|
|
279
|
-
clearInterval(interval_1);
|
|
280
|
-
else
|
|
281
|
-
tracking_config = getFeatureValue('tracking-buttons-config', {});
|
|
282
|
-
}, 1000);
|
|
283
|
-
}
|
|
284
|
-
return [3 /*break*/, 4];
|
|
285
|
-
case 3:
|
|
286
|
-
err_1 = _2.sent();
|
|
287
|
-
console.warn('Analytics: Failed to initialize', err_1);
|
|
288
|
-
return [3 /*break*/, 4];
|
|
289
|
-
case 4: return [2 /*return*/];
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
});
|
|
293
|
-
};
|
|
294
|
-
var setAttributes = function (_a) {
|
|
295
|
-
var country = _a.country, user_language = _a.user_language, device_language = _a.device_language, device_type = _a.device_type, account_type = _a.account_type, user_id = _a.user_id, anonymous_id = _a.anonymous_id, app_id = _a.app_id, utm_source = _a.utm_source, utm_medium = _a.utm_medium, utm_campaign = _a.utm_campaign, is_authorised = _a.is_authorised, residence_country = _a.residence_country, url = _a.url, domain = _a.domain, geo_location = _a.geo_location, loggedIn = _a.loggedIn, network_downlink = _a.network_downlink, network_rtt = _a.network_rtt, network_type = _a.network_type, account_currency = _a.account_currency, account_mode = _a.account_mode;
|
|
296
|
-
if (!_rudderstack && !_posthog)
|
|
297
|
-
return;
|
|
298
|
-
var user_identity = user_id !== null && user_id !== void 0 ? user_id : getId();
|
|
299
|
-
if (_growthbook) {
|
|
300
|
-
var config = __assign(__assign({ country: country, residence_country: residence_country, user_language: user_language, device_language: device_language, device_type: device_type, utm_source: utm_source, utm_medium: utm_medium, utm_campaign: utm_campaign, is_authorised: is_authorised, url: url, domain: domain, loggedIn: loggedIn }, (user_id && !isUUID(user_id) && { user_id: user_id })), { anonymous_id: anonymous_id });
|
|
301
|
-
if (user_identity) {
|
|
302
|
-
config.id = user_identity;
|
|
303
|
-
config.user_id = user_identity;
|
|
304
|
-
}
|
|
305
|
-
_growthbook.setAttributes(config);
|
|
306
|
-
}
|
|
307
|
-
core_data = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, core_data), (country !== undefined && { country: country })), (geo_location !== undefined && { geo_location: geo_location })), (user_language !== undefined && { user_language: user_language })), (account_type !== undefined && { account_type: account_type })), (app_id !== undefined && { app_id: app_id })), (residence_country !== undefined && { residence_country: residence_country })), (device_type !== undefined && { device_type: device_type })), (url !== undefined && { url: url })), (loggedIn !== undefined && { loggedIn: loggedIn })), (network_downlink !== undefined && { network_downlink: network_downlink })), (network_rtt !== undefined && { network_rtt: network_rtt })), (network_type !== undefined && { network_type: network_type })), (user_id !== undefined && !isUUID(user_id) && { user_id: user_id })), (anonymous_id !== undefined && { anonymous_id: anonymous_id })), (account_currency !== undefined && { account_currency: account_currency })), (account_mode !== undefined && { account_mode: account_mode }));
|
|
308
|
-
};
|
|
309
|
-
var getFeatureState = function (id) { var _a, _b; return (_b = (_a = _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureState(id)) === null || _a === void 0 ? void 0 : _a.experimentResult) === null || _b === void 0 ? void 0 : _b.name; };
|
|
310
|
-
var getFeatureValue = function (id, defaultValue) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureValue(id, defaultValue); };
|
|
311
|
-
var getGrowthbookStatus = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
312
|
-
switch (_a.label) {
|
|
313
|
-
case 0: return [4 /*yield*/, (_growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getStatus())];
|
|
314
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
315
|
-
}
|
|
316
|
-
}); }); };
|
|
317
|
-
var isFeatureOn = function (key) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.isOn(key); };
|
|
318
|
-
var setUrl = function (href) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.setUrl(href); };
|
|
319
|
-
var getId = function () {
|
|
320
|
-
var userId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.getUserId()) || '';
|
|
321
|
-
return userId && !isUUID(userId) ? userId : '';
|
|
322
|
-
};
|
|
323
|
-
var getAnonymousId = function () { return (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getAnonymousId()) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.getAnonymousId()) || ''; };
|
|
324
|
-
var pageView = function (current_page, platform, properties) {
|
|
325
|
-
if (platform === void 0) { platform = 'Deriv App'; }
|
|
326
|
-
if (_enableBotFiltering && isLikelyBot())
|
|
327
|
-
return;
|
|
328
|
-
if (!_rudderstack && !_posthog) {
|
|
329
|
-
cachePageViewToCookie(current_page, __assign({ platform: platform }, properties));
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
var userId = getId();
|
|
333
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, userId, properties);
|
|
334
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.pageView(current_page, platform, userId, properties);
|
|
335
|
-
};
|
|
336
|
-
var identifyEvent = function (user_id) {
|
|
337
|
-
var stored_user_id = user_id || getId();
|
|
338
|
-
if (!stored_user_id)
|
|
339
|
-
return;
|
|
340
|
-
if ((_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.has_initialized) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.has_initialized)) {
|
|
341
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(stored_user_id, { language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en' });
|
|
342
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.identifyEvent(stored_user_id, { language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en' });
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
if (!_pending_identify_calls.includes(stored_user_id)) {
|
|
346
|
-
_pending_identify_calls.push(stored_user_id);
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
var reset = function () {
|
|
350
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.reset();
|
|
351
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.reset();
|
|
352
|
-
};
|
|
353
|
-
var isV2Payload = function (payload) {
|
|
354
|
-
return 'event_metadata' in payload || 'cta_information' in payload || 'error' in payload;
|
|
355
|
-
};
|
|
356
|
-
var trackEvent = function (event, analytics_data) {
|
|
357
|
-
if (_enableBotFiltering && isLikelyBot())
|
|
358
|
-
return;
|
|
359
|
-
var userId = getId();
|
|
360
|
-
var final_payload = {};
|
|
361
|
-
if (isV2Payload(analytics_data)) {
|
|
362
|
-
var v2_data = analytics_data;
|
|
363
|
-
final_payload = __assign(__assign({}, v2_data), { event_metadata: __assign(__assign(__assign({}, core_data), (userId && { user_id: userId })), v2_data.event_metadata) });
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
final_payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
|
|
367
|
-
}
|
|
368
|
-
var hasInitializedProvider = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.has_initialized) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.has_initialized);
|
|
369
|
-
if (!navigator.onLine || !hasInitializedProvider) {
|
|
370
|
-
if (!hasInitializedProvider) {
|
|
371
|
-
cacheEventToCookie(event, final_payload);
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
offline_event_cache.push({ event: event, payload: final_payload });
|
|
375
|
-
}
|
|
376
|
-
return;
|
|
377
|
-
}
|
|
378
|
-
if (offline_event_cache.length > 0) {
|
|
379
|
-
offline_event_cache.forEach(function (cache) {
|
|
380
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(cache.event, cache.payload);
|
|
381
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.track(cache.event, cache.payload);
|
|
382
|
-
});
|
|
383
|
-
offline_event_cache = [];
|
|
384
|
-
}
|
|
385
|
-
var shouldTrack = !(event in tracking_config) || tracking_config[event];
|
|
386
|
-
if (shouldTrack) {
|
|
387
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, final_payload);
|
|
388
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.track(event, final_payload);
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
var getInstances = function () { return ({ ab: _growthbook, tracking: _rudderstack, posthog: _posthog }); };
|
|
392
|
-
var AnalyticsInstance = {
|
|
393
|
-
initialise: initialise,
|
|
394
|
-
setAttributes: setAttributes,
|
|
395
|
-
identifyEvent: identifyEvent,
|
|
396
|
-
getFeatureState: getFeatureState,
|
|
397
|
-
getFeatureValue: getFeatureValue,
|
|
398
|
-
getGrowthbookStatus: getGrowthbookStatus,
|
|
399
|
-
isFeatureOn: isFeatureOn,
|
|
400
|
-
setUrl: setUrl,
|
|
401
|
-
getId: getId,
|
|
402
|
-
getAnonymousId: getAnonymousId,
|
|
403
|
-
trackEvent: trackEvent,
|
|
404
|
-
getInstances: getInstances,
|
|
405
|
-
pageView: pageView,
|
|
406
|
-
reset: reset,
|
|
407
|
-
};
|
|
408
|
-
if (typeof window !== 'undefined') {
|
|
409
|
-
window.AnalyticsInstance = AnalyticsInstance;
|
|
410
|
-
}
|
|
411
|
-
return AnalyticsInstance;
|
|
412
|
-
}
|
|
413
|
-
exports.createAnalyticsInstance = createAnalyticsInstance;
|
|
414
|
-
exports.Analytics = createAnalyticsInstance();
|
|
415
|
-
var isUUID = function (str) {
|
|
416
|
-
var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
417
|
-
return uuidRegex.test(str);
|
|
418
|
-
};
|
package/lib/growthbook.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { GrowthBook, InitResponse } from '@growthbook/growthbook';
|
|
2
|
-
import { RudderAnalytics } from '@rudderstack/analytics-js';
|
|
3
|
-
import { TGrowthbookAttributes, TGrowthbookOptions } from './types';
|
|
4
|
-
export type GrowthbookConfigs = {
|
|
5
|
-
'tracking-buttons-config': Record<string, boolean>;
|
|
6
|
-
} & {
|
|
7
|
-
[key: string]: Record<string, boolean> | string | boolean | [];
|
|
8
|
-
};
|
|
9
|
-
export declare class Growthbook {
|
|
10
|
-
analytics: RudderAnalytics;
|
|
11
|
-
GrowthBook: GrowthBook<GrowthbookConfigs>;
|
|
12
|
-
private static _instance;
|
|
13
|
-
isLoaded: boolean;
|
|
14
|
-
status: void | InitResponse;
|
|
15
|
-
constructor(clientKey: string, decryptionKey: string, growthbookOptions?: TGrowthbookOptions);
|
|
16
|
-
static getGrowthBookInstance: (clientKey: string, decryptionKey?: string, growthbookOptions?: TGrowthbookOptions) => Growthbook;
|
|
17
|
-
reapplyExperiment(url?: string): void;
|
|
18
|
-
private waitForIsLoaded;
|
|
19
|
-
setAttributes: ({ id, country, user_language, device_language, device_type, utm_source, utm_medium, utm_campaign, is_authorised, url, domain, utm_content, residence_country, loggedIn, network_type, network_downlink, user_id, anonymous_id, account_mode, }: TGrowthbookAttributes) => void;
|
|
20
|
-
getFeatureValue: <K extends string | number, V extends GrowthbookConfigs[K]>(key: K, defaultValue: V) => import("@growthbook/growthbook").WidenPrimitives<V>;
|
|
21
|
-
getStatus: () => Promise<{
|
|
22
|
-
isLoaded: boolean;
|
|
23
|
-
status: void | InitResponse;
|
|
24
|
-
}>;
|
|
25
|
-
getFeatureState: (id: string) => import("@growthbook/growthbook").FeatureResult<string | boolean | Record<string, boolean> | [] | null>;
|
|
26
|
-
setUrl: (href: string) => Promise<void>;
|
|
27
|
-
isOn: (key: string) => boolean;
|
|
28
|
-
init: () => Promise<void>;
|
|
29
|
-
}
|