@deriv-com/analytics 1.34.0 → 1.35.1
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 +5 -5
- package/dist/analytics.bundle.js +1 -1
- package/lib/analytics.d.ts +8 -6
- package/lib/analytics.js +186 -93
- package/lib/growthbook.js +2 -2
- package/lib/posthog.d.ts +4 -39
- package/lib/posthog.js +74 -192
- package/lib/rudderstack.d.ts +4 -30
- package/lib/rudderstack.js +37 -119
- package/package.json +2 -2
package/lib/analytics.d.ts
CHANGED
|
@@ -10,15 +10,15 @@ declare global {
|
|
|
10
10
|
type Options = {
|
|
11
11
|
growthbookKey?: string;
|
|
12
12
|
growthbookDecryptionKey?: string;
|
|
13
|
-
rudderstackKey
|
|
13
|
+
rudderstackKey?: string;
|
|
14
14
|
posthogKey?: string;
|
|
15
15
|
posthogHost?: string;
|
|
16
16
|
posthogConfig?: PostHogConfig;
|
|
17
17
|
growthbookOptions?: TGrowthbookOptions;
|
|
18
|
-
|
|
18
|
+
enableBotFiltering?: boolean;
|
|
19
19
|
};
|
|
20
20
|
export declare function createAnalyticsInstance(options?: Options): {
|
|
21
|
-
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, posthogKey, posthogHost, posthogConfig, growthbookOptions,
|
|
21
|
+
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, posthogKey, posthogHost, posthogConfig, growthbookOptions, enableBotFiltering, }: Options) => Promise<void>;
|
|
22
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
23
|
identifyEvent: (user_id?: string) => void;
|
|
24
24
|
getFeatureState: (id: string) => string | undefined;
|
|
@@ -30,17 +30,18 @@ export declare function createAnalyticsInstance(options?: Options): {
|
|
|
30
30
|
isFeatureOn: (key: string) => boolean;
|
|
31
31
|
setUrl: (href: string) => Promise<void>;
|
|
32
32
|
getId: () => string;
|
|
33
|
+
getAnonymousId: () => string;
|
|
33
34
|
trackEvent: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, analytics_data: TAllEvents[T]) => void;
|
|
34
35
|
getInstances: () => {
|
|
35
36
|
ab: Growthbook;
|
|
36
37
|
tracking: RudderStack;
|
|
37
38
|
posthog: PostHogAnalytics;
|
|
38
39
|
};
|
|
39
|
-
pageView: (current_page: string, platform?: string, properties?:
|
|
40
|
+
pageView: (current_page: string, platform?: string, properties?: Record<string, unknown>) => void;
|
|
40
41
|
reset: () => void;
|
|
41
42
|
};
|
|
42
43
|
export declare const Analytics: {
|
|
43
|
-
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, posthogKey, posthogHost, posthogConfig, growthbookOptions,
|
|
44
|
+
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, posthogKey, posthogHost, posthogConfig, growthbookOptions, enableBotFiltering, }: Options) => Promise<void>;
|
|
44
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;
|
|
45
46
|
identifyEvent: (user_id?: string) => void;
|
|
46
47
|
getFeatureState: (id: string) => string | undefined;
|
|
@@ -52,13 +53,14 @@ export declare const Analytics: {
|
|
|
52
53
|
isFeatureOn: (key: string) => boolean;
|
|
53
54
|
setUrl: (href: string) => Promise<void>;
|
|
54
55
|
getId: () => string;
|
|
56
|
+
getAnonymousId: () => string;
|
|
55
57
|
trackEvent: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, analytics_data: TAllEvents[T]) => void;
|
|
56
58
|
getInstances: () => {
|
|
57
59
|
ab: Growthbook;
|
|
58
60
|
tracking: RudderStack;
|
|
59
61
|
posthog: PostHogAnalytics;
|
|
60
62
|
};
|
|
61
|
-
pageView: (current_page: string, platform?: string, properties?:
|
|
63
|
+
pageView: (current_page: string, platform?: string, properties?: Record<string, unknown>) => void;
|
|
62
64
|
reset: () => void;
|
|
63
65
|
};
|
|
64
66
|
export {};
|
package/lib/analytics.js
CHANGED
|
@@ -56,9 +56,132 @@ var rudderstack_1 = require("./rudderstack");
|
|
|
56
56
|
var posthog_1 = require("./posthog");
|
|
57
57
|
var js_cookie_1 = __importDefault(require("js-cookie"));
|
|
58
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
|
+
};
|
|
59
106
|
function createAnalyticsInstance(options) {
|
|
60
107
|
var _this = this;
|
|
61
|
-
var _growthbook, _rudderstack, _posthog, core_data = {}, tracking_config = {},
|
|
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
|
+
};
|
|
62
185
|
var getClientCountry = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
63
186
|
var countryFromCloudflare, countryFromCookie, websiteStatus, countryFromWebsiteStatus;
|
|
64
187
|
var _a;
|
|
@@ -74,24 +197,35 @@ function createAnalyticsInstance(options) {
|
|
|
74
197
|
try {
|
|
75
198
|
countryFromWebsiteStatus = ((_a = JSON.parse(websiteStatus)) === null || _a === void 0 ? void 0 : _a.clients_country) || '';
|
|
76
199
|
}
|
|
77
|
-
catch (
|
|
78
|
-
console.
|
|
200
|
+
catch (err) {
|
|
201
|
+
console.warn('Analytics: Failed to parse website_status cookie', err);
|
|
79
202
|
}
|
|
80
203
|
}
|
|
81
204
|
return [2 /*return*/, countryFromCookie || countryFromWebsiteStatus || countryFromCloudflare];
|
|
82
205
|
}
|
|
83
206
|
});
|
|
84
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
|
+
};
|
|
85
218
|
var initialise = function (_a) {
|
|
86
|
-
var growthbookKey = _a.growthbookKey, growthbookDecryptionKey = _a.growthbookDecryptionKey, rudderstackKey = _a.rudderstackKey, posthogKey = _a.posthogKey, posthogHost = _a.posthogHost, posthogConfig = _a.posthogConfig, growthbookOptions = _a.growthbookOptions, _b = _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;
|
|
87
220
|
return __awaiter(_this, void 0, void 0, function () {
|
|
88
|
-
var country, _c, interval_1,
|
|
221
|
+
var country, _c, anonymousId, anonId, interval_1, err_1;
|
|
89
222
|
var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
|
|
90
223
|
var _0, _1;
|
|
91
224
|
return __generator(this, function (_2) {
|
|
92
225
|
switch (_2.label) {
|
|
93
226
|
case 0:
|
|
94
227
|
_2.trys.push([0, 3, , 4]);
|
|
228
|
+
_enableBotFiltering = enableBotFiltering;
|
|
95
229
|
_c = ((_d = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _d === void 0 ? void 0 : _d.country);
|
|
96
230
|
if (_c) return [3 /*break*/, 2];
|
|
97
231
|
return [4 /*yield*/, getClientCountry()];
|
|
@@ -100,36 +234,14 @@ function createAnalyticsInstance(options) {
|
|
|
100
234
|
_2.label = 2;
|
|
101
235
|
case 2:
|
|
102
236
|
country = _c;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (userId) {
|
|
107
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(userId, {
|
|
108
|
-
language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en',
|
|
109
|
-
});
|
|
110
|
-
// Also identify in PostHog if initialized
|
|
111
|
-
if (_posthog === null || _posthog === void 0 ? void 0 : _posthog.has_initialized) {
|
|
112
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.identifyEvent(userId, {
|
|
113
|
-
language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en',
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
_pending_identify_calls = [];
|
|
119
|
-
});
|
|
120
|
-
// Initialize PostHog only if key is provided
|
|
237
|
+
if (rudderstackKey) {
|
|
238
|
+
_rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey, onSdkLoaded);
|
|
239
|
+
}
|
|
121
240
|
if (posthogKey) {
|
|
122
|
-
_posthog = posthog_1.PostHogAnalytics.getPostHogInstance(posthogKey, posthogHost || 'https://ph.deriv.com',
|
|
123
|
-
_pending_identify_calls.forEach(function (userId) {
|
|
124
|
-
if (userId) {
|
|
125
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.identifyEvent(userId, {
|
|
126
|
-
language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en',
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}, posthogConfig);
|
|
241
|
+
_posthog = posthog_1.PostHogAnalytics.getPostHogInstance(posthogKey, posthogHost || 'https://ph.deriv.com', onSdkLoaded, posthogConfig);
|
|
131
242
|
}
|
|
132
|
-
if ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && Object.keys(growthbookOptions.attributes).length > 0)
|
|
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());
|
|
133
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) && {
|
|
134
246
|
user_language: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.user_language,
|
|
135
247
|
})), (((_f = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _f === void 0 ? void 0 : _f.account_type) && {
|
|
@@ -138,9 +250,7 @@ function createAnalyticsInstance(options) {
|
|
|
138
250
|
residence_country: (_j = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _j === void 0 ? void 0 : _j.residence_country,
|
|
139
251
|
})), (((_k = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _k === void 0 ? void 0 : _k.device_type) && {
|
|
140
252
|
device_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.device_type,
|
|
141
|
-
})), (((_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) && {
|
|
142
|
-
loggedIn: !!((_m = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _m === void 0 ? void 0 : _m.loggedIn),
|
|
143
|
-
})), (((_o = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _o === void 0 ? void 0 : _o.email_hash) && {
|
|
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) && {
|
|
144
254
|
email_hash: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.email_hash,
|
|
145
255
|
})), (((_p = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _p === void 0 ? void 0 : _p.network_type) && {
|
|
146
256
|
network_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_type,
|
|
@@ -151,16 +261,16 @@ function createAnalyticsInstance(options) {
|
|
|
151
261
|
})), (((_s = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _s === void 0 ? void 0 : _s.user_id) &&
|
|
152
262
|
!isUUID((_t = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _t === void 0 ? void 0 : _t.user_id) && {
|
|
153
263
|
user_id: (_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.user_id,
|
|
154
|
-
})), ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && {
|
|
155
|
-
anonymous_id: _rudderstack.getAnonymousId(),
|
|
156
|
-
})), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_currency) && {
|
|
264
|
+
})), (anonymousId && { anonymous_id: anonymousId })), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_currency) && {
|
|
157
265
|
account_currency: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_currency,
|
|
158
266
|
})), (((_w = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _w === void 0 ? void 0 : _w.account_mode) && {
|
|
159
267
|
account_mode: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_mode,
|
|
160
268
|
}));
|
|
269
|
+
}
|
|
161
270
|
growthbookOptions !== null && growthbookOptions !== void 0 ? growthbookOptions : (growthbookOptions = {});
|
|
162
271
|
(_x = growthbookOptions.attributes) !== null && _x !== void 0 ? _x : (growthbookOptions.attributes = {});
|
|
163
|
-
|
|
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);
|
|
164
274
|
(_z = (_1 = growthbookOptions.attributes).country) !== null && _z !== void 0 ? _z : (_1.country = country);
|
|
165
275
|
if (growthbookKey) {
|
|
166
276
|
_growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions);
|
|
@@ -173,8 +283,8 @@ function createAnalyticsInstance(options) {
|
|
|
173
283
|
}
|
|
174
284
|
return [3 /*break*/, 4];
|
|
175
285
|
case 3:
|
|
176
|
-
|
|
177
|
-
console.
|
|
286
|
+
err_1 = _2.sent();
|
|
287
|
+
console.warn('Analytics: Failed to initialize', err_1);
|
|
178
288
|
return [3 /*break*/, 4];
|
|
179
289
|
case 4: return [2 /*return*/];
|
|
180
290
|
}
|
|
@@ -183,10 +293,9 @@ function createAnalyticsInstance(options) {
|
|
|
183
293
|
};
|
|
184
294
|
var setAttributes = function (_a) {
|
|
185
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;
|
|
186
|
-
if (!_rudderstack)
|
|
296
|
+
if (!_rudderstack && !_posthog)
|
|
187
297
|
return;
|
|
188
298
|
var user_identity = user_id !== null && user_id !== void 0 ? user_id : getId();
|
|
189
|
-
// Check if we have Growthbook instance and update its attributes
|
|
190
299
|
if (_growthbook) {
|
|
191
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 });
|
|
192
301
|
if (user_identity) {
|
|
@@ -208,92 +317,75 @@ function createAnalyticsInstance(options) {
|
|
|
208
317
|
var isFeatureOn = function (key) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.isOn(key); };
|
|
209
318
|
var setUrl = function (href) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.setUrl(href); };
|
|
210
319
|
var getId = function () {
|
|
211
|
-
var userId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || '';
|
|
320
|
+
var userId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.getUserId()) || '';
|
|
212
321
|
return userId && !isUUID(userId) ? userId : '';
|
|
213
322
|
};
|
|
214
|
-
|
|
215
|
-
* Pushes page view event to RudderStack and PostHog
|
|
216
|
-
*
|
|
217
|
-
* @param current_page The name or URL of the current page to track the page view event
|
|
218
|
-
*/
|
|
323
|
+
var getAnonymousId = function () { return (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getAnonymousId()) || (_posthog === null || _posthog === void 0 ? void 0 : _posthog.getAnonymousId()) || ''; };
|
|
219
324
|
var pageView = function (current_page, platform, properties) {
|
|
220
325
|
if (platform === void 0) { platform = 'Deriv App'; }
|
|
221
|
-
if (
|
|
326
|
+
if (_enableBotFiltering && isLikelyBot())
|
|
327
|
+
return;
|
|
328
|
+
if (!_rudderstack && !_posthog) {
|
|
329
|
+
cachePageViewToCookie(current_page, __assign({ platform: platform }, properties));
|
|
222
330
|
return;
|
|
331
|
+
}
|
|
223
332
|
var userId = getId();
|
|
224
|
-
// Send to RudderStack
|
|
225
333
|
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, userId, properties);
|
|
226
|
-
// Send to PostHog (if initialized)
|
|
227
334
|
_posthog === null || _posthog === void 0 ? void 0 : _posthog.pageView(current_page, platform, userId, properties);
|
|
228
335
|
};
|
|
229
336
|
var identifyEvent = function (user_id) {
|
|
230
337
|
var stored_user_id = user_id || getId();
|
|
231
|
-
if (
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
// Identify in PostHog (if initialized)
|
|
239
|
-
if (_posthog === null || _posthog === void 0 ? void 0 : _posthog.has_initialized) {
|
|
240
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.identifyEvent(stored_user_id, {
|
|
241
|
-
language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en',
|
|
242
|
-
});
|
|
243
|
-
}
|
|
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' });
|
|
244
343
|
return;
|
|
245
344
|
}
|
|
246
|
-
if (stored_user_id) {
|
|
345
|
+
if (!_pending_identify_calls.includes(stored_user_id)) {
|
|
247
346
|
_pending_identify_calls.push(stored_user_id);
|
|
248
347
|
}
|
|
249
348
|
};
|
|
250
349
|
var reset = function () {
|
|
251
|
-
if (!_rudderstack && !_posthog)
|
|
252
|
-
return;
|
|
253
|
-
// Reset RudderStack
|
|
254
350
|
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.reset();
|
|
255
|
-
// Reset PostHog (if initialized)
|
|
256
351
|
_posthog === null || _posthog === void 0 ? void 0 : _posthog.reset();
|
|
257
352
|
};
|
|
258
353
|
var isV2Payload = function (payload) {
|
|
259
354
|
return 'event_metadata' in payload || 'cta_information' in payload || 'error' in payload;
|
|
260
355
|
};
|
|
261
356
|
var trackEvent = function (event, analytics_data) {
|
|
357
|
+
if (_enableBotFiltering && isLikelyBot())
|
|
358
|
+
return;
|
|
262
359
|
var userId = getId();
|
|
263
360
|
var final_payload = {};
|
|
264
361
|
if (isV2Payload(analytics_data)) {
|
|
265
|
-
// --- V2 LOGIC: Nest Core Data ---
|
|
266
362
|
var v2_data = analytics_data;
|
|
267
363
|
final_payload = __assign(__assign({}, v2_data), { event_metadata: __assign(__assign(__assign({}, core_data), (userId && { user_id: userId })), v2_data.event_metadata) });
|
|
268
364
|
}
|
|
269
365
|
else {
|
|
270
|
-
// --- V1 LOGIC: Flatten Core Data (Backward Compatible) ---
|
|
271
366
|
final_payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
|
|
272
367
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(cache.event, cache.payload);
|
|
278
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.track(cache.event, cache.payload);
|
|
279
|
-
delete event_cache[index];
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
if (event in tracking_config) {
|
|
283
|
-
if (tracking_config[event]) {
|
|
284
|
-
// Send to both RudderStack and PostHog
|
|
285
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, final_payload);
|
|
286
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.track(event, final_payload);
|
|
287
|
-
}
|
|
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);
|
|
288
372
|
}
|
|
289
373
|
else {
|
|
290
|
-
|
|
291
|
-
_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, final_payload);
|
|
292
|
-
_posthog === null || _posthog === void 0 ? void 0 : _posthog.track(event, final_payload);
|
|
374
|
+
offline_event_cache.push({ event: event, payload: final_payload });
|
|
293
375
|
}
|
|
376
|
+
return;
|
|
294
377
|
}
|
|
295
|
-
|
|
296
|
-
|
|
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);
|
|
297
389
|
}
|
|
298
390
|
};
|
|
299
391
|
var getInstances = function () { return ({ ab: _growthbook, tracking: _rudderstack, posthog: _posthog }); };
|
|
@@ -307,6 +399,7 @@ function createAnalyticsInstance(options) {
|
|
|
307
399
|
isFeatureOn: isFeatureOn,
|
|
308
400
|
setUrl: setUrl,
|
|
309
401
|
getId: getId,
|
|
402
|
+
getAnonymousId: getAnonymousId,
|
|
310
403
|
trackEvent: trackEvent,
|
|
311
404
|
getInstances: getInstances,
|
|
312
405
|
pageView: pageView,
|
package/lib/growthbook.js
CHANGED
|
@@ -87,7 +87,7 @@ var Growthbook = /** @class */ (function () {
|
|
|
87
87
|
return __generator(this, function (_a) {
|
|
88
88
|
switch (_a.label) {
|
|
89
89
|
case 0: return [4 /*yield*/, this.GrowthBook.init({ timeout: 2000, streaming: true }).catch(function (err) {
|
|
90
|
-
console.error(err)
|
|
90
|
+
// console.error(err)
|
|
91
91
|
})];
|
|
92
92
|
case 1:
|
|
93
93
|
status = _a.sent();
|
|
@@ -117,7 +117,7 @@ var Growthbook = /** @class */ (function () {
|
|
|
117
117
|
Growthbook.prototype.reapplyExperiment = function (url) {
|
|
118
118
|
var currentUrl = url !== null && url !== void 0 ? url : window.location.href;
|
|
119
119
|
this.GrowthBook.setURL(currentUrl);
|
|
120
|
-
console.log('Route changed, new URL:', currentUrl)
|
|
120
|
+
// console.log('Route changed, new URL:', currentUrl)
|
|
121
121
|
};
|
|
122
122
|
// Utility function to wait for isLoaded to become true
|
|
123
123
|
Growthbook.prototype.waitForIsLoaded = function () {
|
package/lib/posthog.d.ts
CHANGED
|
@@ -12,52 +12,17 @@ export declare class PostHogAnalytics {
|
|
|
12
12
|
rudderstack_anonymous_cookie_key: string;
|
|
13
13
|
private static _instance;
|
|
14
14
|
private onLoadedCallback?;
|
|
15
|
-
constructor(POSTHOG_KEY: string, POSTHOG_HOST?: string,
|
|
16
|
-
static getPostHogInstance: (POSTHOG_KEY: string, POSTHOG_HOST?: string,
|
|
15
|
+
constructor(POSTHOG_KEY: string, POSTHOG_HOST?: string, onLoaded?: () => void, config?: PostHogConfig);
|
|
16
|
+
static getPostHogInstance: (POSTHOG_KEY: string, POSTHOG_HOST?: string, onLoaded?: () => void, config?: PostHogConfig) => PostHogAnalytics;
|
|
17
17
|
getAnonymousId: () => string | undefined;
|
|
18
18
|
setCookieIfNotExists: () => void;
|
|
19
|
-
/**
|
|
20
|
-
* @returns The user ID that was assigned to the user after calling identify event
|
|
21
|
-
*/
|
|
22
19
|
getUserId: () => string;
|
|
23
|
-
/** For caching mechanism, PostHog SDK, first page load */
|
|
24
|
-
handleCachedEvents: () => void;
|
|
25
|
-
/**
|
|
26
|
-
* Transform V2 event payload to PostHog flat structure
|
|
27
|
-
*/
|
|
28
20
|
private transformToPostHogPayload;
|
|
29
|
-
|
|
30
|
-
* Initializes the PostHog SDK using proxy endpoint.
|
|
31
|
-
* For local/staging environment, ensure that `POSTHOG_STAGING_KEY` is set.
|
|
32
|
-
* For production environment, ensure that `POSTHOG_PRODUCTION_KEY` is set.
|
|
33
|
-
*
|
|
34
|
-
* Proxy Setup:
|
|
35
|
-
* - Using https://ph.deriv.com as proxy endpoint (same for staging and production)
|
|
36
|
-
* - Proxy forwards requests to PostHog Cloud
|
|
37
|
-
* - X-headers are configured on the proxy server (not client-side)
|
|
38
|
-
* - See: https://posthog.com/docs/advanced/proxy
|
|
39
|
-
*/
|
|
40
|
-
init: (POSTHOG_KEY: string, POSTHOG_HOST?: string, disableAMD?: boolean, config?: PostHogConfig) => void;
|
|
41
|
-
/**
|
|
42
|
-
*
|
|
43
|
-
* @param user_id The user ID of the user to identify and associate all events with that particular user ID
|
|
44
|
-
* @param payload Additional information passed to identify the user
|
|
45
|
-
*/
|
|
21
|
+
init: (POSTHOG_KEY: string, POSTHOG_HOST?: string, config?: PostHogConfig) => void;
|
|
46
22
|
identifyEvent: (user_id: string, payload: {
|
|
47
23
|
language: string;
|
|
48
24
|
}) => void;
|
|
49
|
-
|
|
50
|
-
* Pushes page view event to PostHog
|
|
51
|
-
*
|
|
52
|
-
* @param current_page The name or URL of the current page to track the page view event
|
|
53
|
-
*/
|
|
54
|
-
pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: {}) => void;
|
|
55
|
-
/**
|
|
56
|
-
* Pushes reset event to PostHog
|
|
57
|
-
*/
|
|
25
|
+
pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: Record<string, unknown>) => void;
|
|
58
26
|
reset: () => void;
|
|
59
|
-
/**
|
|
60
|
-
* Pushes track events to PostHog with automatic payload transformation
|
|
61
|
-
*/
|
|
62
27
|
track: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, payload: TAllEvents[T] & Partial<TCoreAttributes>) => void;
|
|
63
28
|
}
|