@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.
Files changed (75) hide show
  1. package/README.md +853 -72
  2. package/dist/browser/analytics.bundle.global.js +23 -0
  3. package/dist/browser/analytics.bundle.global.js.map +1 -0
  4. package/dist/browser/analytics.esm.mjs +9 -0
  5. package/dist/browser/analytics.esm.mjs.map +1 -0
  6. package/dist/chunk-3LFZFQL4.mjs +3 -0
  7. package/dist/chunk-3LFZFQL4.mjs.map +1 -0
  8. package/dist/chunk-DNCZM4KN.mjs +4 -0
  9. package/dist/chunk-DNCZM4KN.mjs.map +1 -0
  10. package/dist/chunk-EMF3RT6E.mjs +14 -0
  11. package/dist/chunk-EMF3RT6E.mjs.map +1 -0
  12. package/dist/chunk-HQX3Z7PF.mjs +3 -0
  13. package/dist/chunk-HQX3Z7PF.mjs.map +1 -0
  14. package/dist/chunk-KDJ46GSX.mjs +3 -0
  15. package/dist/chunk-KDJ46GSX.mjs.map +1 -0
  16. package/dist/chunk-LKLVBL24.js +3 -0
  17. package/dist/chunk-LKLVBL24.js.map +1 -0
  18. package/dist/chunk-NOEKD4DT.js +4 -0
  19. package/dist/chunk-NOEKD4DT.js.map +1 -0
  20. package/dist/chunk-NZ35MU4O.js +3 -0
  21. package/dist/chunk-NZ35MU4O.js.map +1 -0
  22. package/dist/growthbook-NJGOOJH4.mjs +3 -0
  23. package/dist/growthbook-NJGOOJH4.mjs.map +1 -0
  24. package/dist/index.d.mts +114 -0
  25. package/dist/index.d.ts +114 -0
  26. package/dist/index.js +4 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/index.mjs +4 -0
  29. package/dist/index.mjs.map +1 -0
  30. package/dist/metafile-cjs.json +1 -0
  31. package/dist/metafile-esm.json +1 -0
  32. package/dist/metafile-iife.json +1 -0
  33. package/dist/posthog-D3QL9y8a.d.ts +95 -0
  34. package/dist/posthog-DGiwrEsA.d.mts +95 -0
  35. package/dist/posthog-P3AXUMLM.mjs +6 -0
  36. package/dist/posthog-P3AXUMLM.mjs.map +1 -0
  37. package/dist/providers/growthbook/index.d.mts +73 -0
  38. package/dist/providers/growthbook/index.d.ts +73 -0
  39. package/dist/providers/growthbook/index.js +3 -0
  40. package/dist/providers/growthbook/index.js.map +1 -0
  41. package/dist/providers/growthbook/index.mjs +3 -0
  42. package/dist/providers/growthbook/index.mjs.map +1 -0
  43. package/dist/providers/posthog/index.d.mts +3 -0
  44. package/dist/providers/posthog/index.d.ts +3 -0
  45. package/dist/providers/posthog/index.js +3 -0
  46. package/dist/providers/posthog/index.js.map +1 -0
  47. package/dist/providers/posthog/index.mjs +3 -0
  48. package/dist/providers/posthog/index.mjs.map +1 -0
  49. package/dist/providers/rudderstack/index.d.mts +73 -0
  50. package/dist/providers/rudderstack/index.d.ts +73 -0
  51. package/dist/providers/rudderstack/index.js +2 -0
  52. package/dist/providers/rudderstack/index.js.map +1 -0
  53. package/dist/providers/rudderstack/index.mjs +2 -0
  54. package/dist/providers/rudderstack/index.mjs.map +1 -0
  55. package/{lib/types.d.ts → dist/types-DoMejCXv.d.mts} +18 -29
  56. package/dist/types-DoMejCXv.d.ts +361 -0
  57. package/dist/utils/analytics-cache/index.d.mts +138 -0
  58. package/dist/utils/analytics-cache/index.d.ts +138 -0
  59. package/dist/utils/analytics-cache/index.js +2 -0
  60. package/dist/utils/analytics-cache/index.js.map +1 -0
  61. package/dist/utils/analytics-cache/index.mjs +2 -0
  62. package/dist/utils/analytics-cache/index.mjs.map +1 -0
  63. package/package.json +97 -68
  64. package/dist/analytics.bundle.js +0 -1
  65. package/lib/analytics.d.ts +0 -66
  66. package/lib/analytics.js +0 -418
  67. package/lib/growthbook.d.ts +0 -29
  68. package/lib/growthbook.js +0 -144
  69. package/lib/index.d.ts +0 -2
  70. package/lib/index.js +0 -5
  71. package/lib/posthog.d.ts +0 -28
  72. package/lib/posthog.js +0 -154
  73. package/lib/rudderstack.d.ts +0 -23
  74. package/lib/rudderstack.js +0 -100
  75. package/lib/types.js +0 -2
package/lib/growthbook.js DELETED
@@ -1,144 +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
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.Growthbook = void 0;
51
- var growthbook_1 = require("@growthbook/growthbook");
52
- var analytics_js_1 = require("@rudderstack/analytics-js");
53
- var Growthbook = /** @class */ (function () {
54
- // we have to pass settings due the specific framework implementation
55
- function Growthbook(clientKey, decryptionKey, growthbookOptions) {
56
- if (growthbookOptions === void 0) { growthbookOptions = {}; }
57
- var _this = this;
58
- this.analytics = new analytics_js_1.RudderAnalytics();
59
- this.isLoaded = false;
60
- this.status = undefined;
61
- this.setAttributes = function (_a) {
62
- var id = _a.id, country = _a.country, user_language = _a.user_language, device_language = _a.device_language, device_type = _a.device_type, utm_source = _a.utm_source, utm_medium = _a.utm_medium, utm_campaign = _a.utm_campaign, is_authorised = _a.is_authorised, url = _a.url, domain = _a.domain, utm_content = _a.utm_content, residence_country = _a.residence_country, loggedIn = _a.loggedIn, network_type = _a.network_type, network_downlink = _a.network_downlink, user_id = _a.user_id, anonymous_id = _a.anonymous_id, account_mode = _a.account_mode;
63
- var CURRENT_ATTRIBUTES = _this.GrowthBook.getAttributes();
64
- _this.GrowthBook.setAttributes(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, CURRENT_ATTRIBUTES), { id: id }), (user_id !== undefined && { user_id: user_id })), (anonymous_id !== undefined && { anonymous_id: anonymous_id })), (country !== undefined && { country: country })), (residence_country !== undefined && { residence_country: residence_country })), (user_language !== undefined && { user_language: user_language })), (device_language !== undefined && { device_language: device_language })), (device_type !== undefined && { device_type: device_type })), (utm_source !== undefined && { utm_source: utm_source })), (utm_medium !== undefined && { utm_medium: utm_medium })), (utm_campaign !== undefined && { utm_campaign: utm_campaign })), (is_authorised !== undefined && { is_authorised: is_authorised })), (url !== undefined && { url: url })), (domain !== undefined && { domain: domain })), (utm_content !== undefined && { utm_content: utm_content })), (loggedIn !== undefined && { loggedIn: loggedIn })), (network_type !== undefined && { network_type: network_type })), (network_downlink !== undefined && { network_downlink: network_downlink })), (account_mode !== undefined && { account_mode: account_mode })));
65
- };
66
- this.getFeatureValue = function (key, defaultValue) {
67
- return _this.GrowthBook.getFeatureValue(key, defaultValue);
68
- };
69
- this.getStatus = function () { return __awaiter(_this, void 0, void 0, function () {
70
- return __generator(this, function (_a) {
71
- switch (_a.label) {
72
- case 0: return [4 /*yield*/, this.waitForIsLoaded()];
73
- case 1:
74
- _a.sent();
75
- return [2 /*return*/, {
76
- isLoaded: this.isLoaded,
77
- status: this.status,
78
- }];
79
- }
80
- });
81
- }); };
82
- this.getFeatureState = function (id) { return _this.GrowthBook.evalFeature(id); };
83
- this.setUrl = function (href) { return _this.GrowthBook.setURL(href); };
84
- this.isOn = function (key) { return _this.GrowthBook.isOn(key); };
85
- this.init = function () { return __awaiter(_this, void 0, void 0, function () {
86
- var status;
87
- return __generator(this, function (_a) {
88
- switch (_a.label) {
89
- case 0: return [4 /*yield*/, this.GrowthBook.init({ timeout: 2000, streaming: true }).catch(function (err) {
90
- // console.error(err)
91
- })];
92
- case 1:
93
- status = _a.sent();
94
- this.status = status;
95
- this.isLoaded = true;
96
- return [2 /*return*/];
97
- }
98
- });
99
- }); };
100
- this.GrowthBook = new growthbook_1.GrowthBook(__assign({ apiHost: 'https://cdn.growthbook.io', clientKey: clientKey, decryptionKey: decryptionKey, antiFlicker: false, navigateDelay: 0, antiFlickerTimeout: 3500, subscribeToChanges: true, enableDevMode: window === null || window === void 0 ? void 0 : window.location.hostname.includes('localhost'), trackingCallback: function (experiment, result) {
101
- if (window.dataLayer) {
102
- window.dataLayer.push({
103
- event: 'experiment_viewed',
104
- event_category: 'experiment',
105
- rudder_anonymous_id: _this.analytics.getAnonymousId(),
106
- experiment_id: experiment.key,
107
- variation_id: result.variationId,
108
- });
109
- }
110
- _this.analytics.track('experiment_viewed', {
111
- experimentId: experiment.key,
112
- variationId: result.variationId,
113
- });
114
- } }, growthbookOptions));
115
- this.init();
116
- }
117
- Growthbook.prototype.reapplyExperiment = function (url) {
118
- var currentUrl = url !== null && url !== void 0 ? url : window.location.href;
119
- this.GrowthBook.setURL(currentUrl);
120
- // console.log('Route changed, new URL:', currentUrl)
121
- };
122
- // Utility function to wait for isLoaded to become true
123
- Growthbook.prototype.waitForIsLoaded = function () {
124
- var _this = this;
125
- return new Promise(function (resolve) {
126
- var checkInterval = setInterval(function () {
127
- if (_this.isLoaded) {
128
- clearInterval(checkInterval);
129
- resolve();
130
- }
131
- }, 100);
132
- });
133
- };
134
- // for make instance by singleton
135
- Growthbook.getGrowthBookInstance = function (clientKey, decryptionKey, growthbookOptions) {
136
- if (!Growthbook._instance) {
137
- Growthbook._instance = new Growthbook(clientKey, decryptionKey !== null && decryptionKey !== void 0 ? decryptionKey : '', growthbookOptions);
138
- return Growthbook._instance;
139
- }
140
- return Growthbook._instance;
141
- };
142
- return Growthbook;
143
- }());
144
- exports.Growthbook = Growthbook;
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { Analytics } from './analytics';
2
- export { TEvents } from './types';
package/lib/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Analytics = void 0;
4
- var analytics_1 = require("./analytics");
5
- Object.defineProperty(exports, "Analytics", { enumerable: true, get: function () { return analytics_1.Analytics; } });
package/lib/posthog.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import type { PostHogConfig as PostHogInitConfig } from 'posthog-js';
2
- import { TCoreAttributes, TAllEvents } from './types';
3
- export type PostHogConfig = Omit<Partial<PostHogInitConfig>, 'api_host' | 'ui_host' | 'bootstrap' | 'loaded'> & {
4
- capture_pageview?: boolean;
5
- capture_pageleave?: boolean;
6
- autocapture?: boolean;
7
- };
8
- export declare class PostHogAnalytics {
9
- has_identified: boolean;
10
- has_initialized: boolean;
11
- current_page: string;
12
- rudderstack_anonymous_cookie_key: string;
13
- private static _instance;
14
- private onLoadedCallback?;
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
- getAnonymousId: () => string | undefined;
18
- setCookieIfNotExists: () => void;
19
- getUserId: () => string;
20
- private transformToPostHogPayload;
21
- init: (POSTHOG_KEY: string, POSTHOG_HOST?: string, config?: PostHogConfig) => void;
22
- identifyEvent: (user_id: string, payload: {
23
- language: string;
24
- }) => void;
25
- pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: Record<string, unknown>) => void;
26
- reset: () => void;
27
- track: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, payload: TAllEvents[T] & Partial<TCoreAttributes>) => void;
28
- }
package/lib/posthog.js DELETED
@@ -1,154 +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 __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.PostHogAnalytics = void 0;
18
- var posthog_js_1 = __importDefault(require("posthog-js"));
19
- var uuid_1 = require("uuid");
20
- var PostHogAnalytics = /** @class */ (function () {
21
- function PostHogAnalytics(POSTHOG_KEY, POSTHOG_HOST, onLoaded, config) {
22
- var _this = this;
23
- this.has_identified = false;
24
- this.has_initialized = false;
25
- this.current_page = '';
26
- this.rudderstack_anonymous_cookie_key = 'rudder_anonymous_id';
27
- this.getAnonymousId = function () {
28
- var _a;
29
- return (_a = document.cookie.match('(^|;)\\s*' + _this.rudderstack_anonymous_cookie_key + '\\s*=\\s*([^;]+)')) === null || _a === void 0 ? void 0 : _a.pop();
30
- };
31
- this.setCookieIfNotExists = function () {
32
- var anonymous_id = _this.getAnonymousId();
33
- if (!anonymous_id) {
34
- var hostname_1 = window.location.hostname;
35
- var external_domains = ['webflow.io'];
36
- var is_external_domain = external_domains.some(function (domain) { return hostname_1.endsWith(domain); });
37
- var domain_name = is_external_domain ? hostname_1 : hostname_1.split('.').slice(-2).join('.');
38
- document.cookie = "".concat(_this.rudderstack_anonymous_cookie_key, "=").concat((0, uuid_1.v6)(), "; path=/; Domain=").concat(domain_name, "; max-age=").concat(2 * 365 * 24 * 60 * 60);
39
- }
40
- };
41
- this.getUserId = function () { return posthog_js_1.default.get_distinct_id(); };
42
- this.transformToPostHogPayload = function (payload, maxDepth) {
43
- if (maxDepth === void 0) { maxDepth = 10; }
44
- var transformed = {};
45
- var flatten = function (obj, depth) {
46
- if (depth === void 0) { depth = 0; }
47
- if (depth > maxDepth)
48
- return;
49
- if (obj === null || obj === undefined)
50
- return;
51
- Object.keys(obj).forEach(function (key) {
52
- var value = obj[key];
53
- if (Array.isArray(value)) {
54
- transformed[key] = value;
55
- }
56
- else if (typeof value === 'object' && value !== null) {
57
- flatten(value, depth + 1);
58
- }
59
- else if (typeof value === 'string') {
60
- var trimmed = value.trim();
61
- if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||
62
- (trimmed.startsWith('[') && trimmed.endsWith(']'))) {
63
- try {
64
- var parsed = JSON.parse(trimmed);
65
- if (typeof parsed === 'object' && parsed !== null) {
66
- flatten(parsed, depth + 1);
67
- }
68
- else {
69
- transformed[key] = value;
70
- }
71
- }
72
- catch (_a) {
73
- transformed[key] = value;
74
- }
75
- }
76
- else {
77
- transformed[key] = value;
78
- }
79
- }
80
- else {
81
- transformed[key] = value;
82
- }
83
- });
84
- };
85
- flatten(payload);
86
- return Object.fromEntries(Object.entries(transformed).filter(function (_a) {
87
- var _ = _a[0], value = _a[1];
88
- return value !== undefined;
89
- }));
90
- };
91
- this.init = function (POSTHOG_KEY, POSTHOG_HOST, config) {
92
- if (!POSTHOG_KEY)
93
- return;
94
- _this.setCookieIfNotExists();
95
- var anonymous_id = _this.getAnonymousId();
96
- posthog_js_1.default.init(POSTHOG_KEY, __assign(__assign({ api_host: POSTHOG_HOST || 'https://ph.deriv.com', ui_host: 'https://us.posthog.com', bootstrap: { distinctID: anonymous_id }, capture_pageview: false, capture_pageleave: false, autocapture: true }, config), { loaded: function () {
97
- var _a;
98
- _this.has_initialized = true;
99
- _this.has_identified = posthog_js_1.default.get_distinct_id() !== anonymous_id;
100
- (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
101
- } }));
102
- };
103
- this.identifyEvent = function (user_id, payload) {
104
- var currentUserId = _this.getUserId();
105
- var anonymousId = _this.getAnonymousId();
106
- if (!currentUserId || currentUserId === anonymousId) {
107
- if (anonymousId && currentUserId === anonymousId) {
108
- posthog_js_1.default.alias(user_id, anonymousId);
109
- }
110
- posthog_js_1.default.identify(user_id, payload);
111
- }
112
- _this.has_identified = true;
113
- };
114
- this.pageView = function (current_page, platform, user_id, properties) {
115
- if (platform === void 0) { platform = 'Deriv App'; }
116
- if (!_this.has_initialized || current_page === _this.current_page)
117
- return;
118
- var pageProperties = __assign(__assign({ platform: platform }, (user_id && { user_id: user_id })), properties);
119
- posthog_js_1.default.capture('$pageview', __assign({ $current_url: window.location.href, page_name: current_page }, pageProperties));
120
- _this.current_page = current_page;
121
- };
122
- this.reset = function () {
123
- if (!_this.has_initialized)
124
- return;
125
- posthog_js_1.default.reset();
126
- _this.has_identified = false;
127
- };
128
- this.track = function (event, payload) {
129
- if (!_this.has_initialized)
130
- return;
131
- try {
132
- var transformedPayload = _this.transformToPostHogPayload(payload);
133
- var clean_payload = Object.fromEntries(Object.entries(transformedPayload).filter(function (_a) {
134
- var _ = _a[0], value = _a[1];
135
- return value !== undefined;
136
- }));
137
- posthog_js_1.default.capture(event, clean_payload);
138
- }
139
- catch (err) {
140
- console.warn('PostHog: Failed to track event', err);
141
- }
142
- };
143
- this.onLoadedCallback = onLoaded;
144
- this.init(POSTHOG_KEY, POSTHOG_HOST, config);
145
- }
146
- PostHogAnalytics.getPostHogInstance = function (POSTHOG_KEY, POSTHOG_HOST, onLoaded, config) {
147
- if (!PostHogAnalytics._instance) {
148
- PostHogAnalytics._instance = new PostHogAnalytics(POSTHOG_KEY, POSTHOG_HOST, onLoaded, config);
149
- }
150
- return PostHogAnalytics._instance;
151
- };
152
- return PostHogAnalytics;
153
- }());
154
- exports.PostHogAnalytics = PostHogAnalytics;
@@ -1,23 +0,0 @@
1
- import { RudderAnalytics } from '@rudderstack/analytics-js';
2
- import { TCoreAttributes, TAllEvents } from './types';
3
- export declare class RudderStack {
4
- analytics: RudderAnalytics;
5
- has_identified: boolean;
6
- has_initialized: boolean;
7
- current_page: string;
8
- rudderstack_anonymous_cookie_key: string;
9
- private static _instance;
10
- private onLoadedCallback?;
11
- constructor(RUDDERSTACK_KEY: string, onLoaded?: () => void);
12
- static getRudderStackInstance: (RUDDERSTACK_KEY: string, onLoaded?: () => void) => RudderStack;
13
- getAnonymousId: () => string | undefined;
14
- setCookieIfNotExists: () => void;
15
- getUserId: () => (string | null) | undefined;
16
- init: (RUDDERSTACK_KEY: string) => void;
17
- identifyEvent: (user_id: string, payload: {
18
- language: string;
19
- }) => void;
20
- pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: Record<string, unknown>) => void;
21
- reset: () => void;
22
- track: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, payload: TAllEvents[T] & Partial<TCoreAttributes>) => void;
23
- }
@@ -1,100 +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
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.RudderStack = void 0;
15
- var analytics_js_1 = require("@rudderstack/analytics-js");
16
- var uuid_1 = require("uuid");
17
- var RudderStack = /** @class */ (function () {
18
- function RudderStack(RUDDERSTACK_KEY, onLoaded) {
19
- var _this = this;
20
- this.analytics = new analytics_js_1.RudderAnalytics();
21
- this.has_identified = false;
22
- this.has_initialized = false;
23
- this.current_page = '';
24
- this.rudderstack_anonymous_cookie_key = 'rudder_anonymous_id';
25
- this.getAnonymousId = function () {
26
- var _a;
27
- return (_a = document.cookie.match('(^|;)\\s*' + _this.rudderstack_anonymous_cookie_key + '\\s*=\\s*([^;]+)')) === null || _a === void 0 ? void 0 : _a.pop();
28
- };
29
- this.setCookieIfNotExists = function () {
30
- var anonymous_id = _this.getAnonymousId();
31
- if (!anonymous_id) {
32
- var hostname_1 = window.location.hostname;
33
- var external_domains = ['webflow.io'];
34
- var is_external_domain = external_domains.some(function (domain) { return hostname_1.endsWith(domain); });
35
- var domain_name = is_external_domain ? hostname_1 : hostname_1.split('.').slice(-2).join('.');
36
- document.cookie = "".concat(_this.rudderstack_anonymous_cookie_key, "=").concat((0, uuid_1.v6)(), "; path=/; Domain=").concat(domain_name, "; max-age=").concat(2 * 365 * 24 * 60 * 60);
37
- }
38
- };
39
- this.getUserId = function () { return _this.analytics.getUserId(); };
40
- this.init = function (RUDDERSTACK_KEY) {
41
- if (!RUDDERSTACK_KEY)
42
- return;
43
- _this.setCookieIfNotExists();
44
- _this.analytics.load(RUDDERSTACK_KEY, 'https://deriv-dataplane.rudderstack.com', {
45
- externalAnonymousIdCookieName: _this.rudderstack_anonymous_cookie_key,
46
- onLoaded: function () {
47
- var _a;
48
- _this.has_initialized = true;
49
- _this.has_identified = !!_this.getUserId();
50
- (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
51
- },
52
- });
53
- };
54
- this.identifyEvent = function (user_id, payload) {
55
- var currentUserId = _this.getUserId();
56
- if (!currentUserId) {
57
- _this.analytics.identify(user_id, payload);
58
- }
59
- _this.has_identified = true;
60
- };
61
- this.pageView = function (current_page, platform, user_id, properties) {
62
- if (platform === void 0) { platform = 'Deriv App'; }
63
- if (!_this.has_initialized || current_page === _this.current_page)
64
- return;
65
- var pageProperties = __assign(__assign({}, (user_id && { user_id: user_id })), properties);
66
- _this.analytics.page(platform, current_page, pageProperties);
67
- _this.current_page = current_page;
68
- };
69
- this.reset = function () {
70
- if (!_this.has_initialized)
71
- return;
72
- _this.analytics.reset();
73
- _this.has_identified = false;
74
- };
75
- this.track = function (event, payload) {
76
- if (!_this.has_initialized)
77
- return;
78
- try {
79
- var clean_payload = Object.fromEntries(Object.entries(payload).filter(function (_a) {
80
- var _ = _a[0], value = _a[1];
81
- return value !== undefined;
82
- }));
83
- _this.analytics.track(event, clean_payload);
84
- }
85
- catch (err) {
86
- console.warn('RudderStack: Failed to track event', err);
87
- }
88
- };
89
- this.onLoadedCallback = onLoaded;
90
- this.init(RUDDERSTACK_KEY);
91
- }
92
- RudderStack.getRudderStackInstance = function (RUDDERSTACK_KEY, onLoaded) {
93
- if (!RudderStack._instance) {
94
- RudderStack._instance = new RudderStack(RUDDERSTACK_KEY, onLoaded);
95
- }
96
- return RudderStack._instance;
97
- };
98
- return RudderStack;
99
- }());
100
- exports.RudderStack = RudderStack;
package/lib/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });