@deriv-com/analytics 1.4.13 → 1.5.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 +14 -0
- package/lib/analytics.d.ts +8 -6
- package/lib/analytics.js +5 -4
- package/lib/growthbook.d.ts +5 -5
- package/lib/growthbook.js +8 -14
- package/lib/types.d.ts +2 -1
- package/package.json +2 -2
package/README.md
CHANGED
@@ -29,6 +29,20 @@ Analytics?.initialise({
|
|
29
29
|
growthbookDecryptionKey: process.env.GROWTHBOOK_DECRYPTION_KEY, // optional key to enable A/B tests
|
30
30
|
// mandatory key to enable userevent tracking
|
31
31
|
rudderstackKey: RUDDERSTACK_KEY,
|
32
|
+
|
33
|
+
growthbookOptions: {
|
34
|
+
// optional options for overriding growthbook default options
|
35
|
+
// if you want e.g
|
36
|
+
antiFlicker: false,
|
37
|
+
navigateDelay: 0,
|
38
|
+
antiFlickerTimeout: 3500,
|
39
|
+
subscribeToChanges: true,
|
40
|
+
enableDevMode: window?.location.hostname.includes('localhost'),
|
41
|
+
trackingCallback: (experiment, result) => {
|
42
|
+
console.log('Tracking callback', experiment, result)
|
43
|
+
}
|
44
|
+
navigate: (url) => window.location.href = url,
|
45
|
+
}
|
32
46
|
})
|
33
47
|
```
|
34
48
|
|
package/lib/analytics.d.ts
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
+
import type { Context } from '@growthbook/growthbook';
|
1
2
|
import { Growthbook, GrowthbookConfigs } from './growthbook';
|
2
3
|
import { RudderStack } from './rudderstack';
|
3
4
|
import { TCoreAttributes, TEvents } from './types';
|
4
5
|
type Options = {
|
5
6
|
growthbookKey?: string;
|
7
|
+
growthbookOptions?: Partial<Context>;
|
6
8
|
growthbookDecryptionKey?: string;
|
7
9
|
rudderstackKey: string;
|
8
10
|
};
|
9
11
|
export declare function createAnalyticsInstance(options?: Options): {
|
10
|
-
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey }: Options) => void;
|
11
|
-
setAttributes: ({ country, user_language, device_language, device_type, account_type, user_id, app_id, utm_source, utm_medium, utm_campaign, is_authorised, }: TCoreAttributes) => void;
|
12
|
+
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, growthbookOptions }: Options) => void;
|
13
|
+
setAttributes: ({ country, user_language, device_language, device_type, account_type, user_id, app_id, utm_source, utm_medium, utm_campaign, is_authorised, url, }: TCoreAttributes) => void;
|
12
14
|
identifyEvent: () => void;
|
13
15
|
getFeatureState: (id: string) => string | undefined;
|
14
16
|
getFeatureValue: <K extends string | number, V extends GrowthbookConfigs[K]>(id: K, defaultValue: V) => import("@growthbook/growthbook").WidenPrimitives<V>;
|
15
17
|
isFeatureOn: (key: string) => boolean;
|
16
|
-
setUrl: (href: string) => void
|
18
|
+
setUrl: (href: string) => Promise<void>;
|
17
19
|
getId: () => string;
|
18
20
|
trackEvent: <T extends keyof TEvents>(event: T, analytics_data: TEvents[T]) => void;
|
19
21
|
getInstances: () => {
|
@@ -24,13 +26,13 @@ export declare function createAnalyticsInstance(options?: Options): {
|
|
24
26
|
reset: () => void;
|
25
27
|
};
|
26
28
|
export declare const Analytics: {
|
27
|
-
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey }: Options) => void;
|
28
|
-
setAttributes: ({ country, user_language, device_language, device_type, account_type, user_id, app_id, utm_source, utm_medium, utm_campaign, is_authorised, }: TCoreAttributes) => void;
|
29
|
+
initialise: ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, growthbookOptions }: Options) => void;
|
30
|
+
setAttributes: ({ country, user_language, device_language, device_type, account_type, user_id, app_id, utm_source, utm_medium, utm_campaign, is_authorised, url, }: TCoreAttributes) => void;
|
29
31
|
identifyEvent: () => void;
|
30
32
|
getFeatureState: (id: string) => string | undefined;
|
31
33
|
getFeatureValue: <K extends string | number, V extends GrowthbookConfigs[K]>(id: K, defaultValue: V) => import("@growthbook/growthbook").WidenPrimitives<V>;
|
32
34
|
isFeatureOn: (key: string) => boolean;
|
33
|
-
setUrl: (href: string) => void
|
35
|
+
setUrl: (href: string) => Promise<void>;
|
34
36
|
getId: () => string;
|
35
37
|
trackEvent: <T extends keyof TEvents>(event: T, analytics_data: TEvents[T]) => void;
|
36
38
|
getInstances: () => {
|
package/lib/analytics.js
CHANGED
@@ -17,10 +17,10 @@ var rudderstack_1 = require("./rudderstack");
|
|
17
17
|
function createAnalyticsInstance(options) {
|
18
18
|
var _growthbook, _rudderstack, core_data = {}, tracking_config = {}, offline_cache = {};
|
19
19
|
var initialise = function (_a) {
|
20
|
-
var growthbookKey = _a.growthbookKey, growthbookDecryptionKey = _a.growthbookDecryptionKey, rudderstackKey = _a.rudderstackKey;
|
20
|
+
var growthbookKey = _a.growthbookKey, growthbookDecryptionKey = _a.growthbookDecryptionKey, rudderstackKey = _a.rudderstackKey, growthbookOptions = _a.growthbookOptions;
|
21
21
|
_rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey);
|
22
22
|
if (growthbookKey && growthbookDecryptionKey) {
|
23
|
-
_growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey);
|
23
|
+
_growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions);
|
24
24
|
var interval_1 = setInterval(function () {
|
25
25
|
if (Object.keys(tracking_config).length > 0)
|
26
26
|
clearInterval(interval_1);
|
@@ -30,7 +30,7 @@ function createAnalyticsInstance(options) {
|
|
30
30
|
}
|
31
31
|
};
|
32
32
|
var setAttributes = function (_a) {
|
33
|
-
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, 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;
|
33
|
+
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, 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, url = _a.url;
|
34
34
|
if (!_growthbook && !_rudderstack)
|
35
35
|
return;
|
36
36
|
var user_identity = user_id !== null && user_id !== void 0 ? user_id : getId();
|
@@ -46,9 +46,10 @@ function createAnalyticsInstance(options) {
|
|
46
46
|
utm_medium: utm_medium,
|
47
47
|
utm_campaign: utm_campaign,
|
48
48
|
is_authorised: is_authorised,
|
49
|
+
url: url,
|
49
50
|
});
|
50
51
|
}
|
51
|
-
core_data = __assign(__assign(__assign(__assign(__assign({}, (user_language !== undefined && { user_language: user_language })), (account_type !== undefined && { account_type: account_type })), (app_id !== undefined && { app_id: app_id })), (device_type !== undefined && { device_type: device_type })), (user_identity !== undefined && { user_identity: user_identity }));
|
52
|
+
core_data = __assign(__assign(__assign(__assign(__assign(__assign({}, (user_language !== undefined && { user_language: user_language })), (account_type !== undefined && { account_type: account_type })), (app_id !== undefined && { app_id: app_id })), (device_type !== undefined && { device_type: device_type })), (user_identity !== undefined && { user_identity: user_identity })), (url !== undefined && { url: url }));
|
52
53
|
};
|
53
54
|
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; };
|
54
55
|
var getFeatureValue = function (id, defaultValue) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureValue(id, defaultValue); };
|
package/lib/growthbook.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { GrowthBook } from '@growthbook/growthbook';
|
1
|
+
import { Context, GrowthBook } from '@growthbook/growthbook';
|
2
2
|
import { TGrowthbookAttributes } from './types';
|
3
3
|
export type GrowthbookConfigs = {
|
4
4
|
'tracking-buttons-config': Record<string, boolean>;
|
@@ -8,12 +8,12 @@ export type GrowthbookConfigs = {
|
|
8
8
|
export declare class Growthbook {
|
9
9
|
GrowthBook: GrowthBook<GrowthbookConfigs>;
|
10
10
|
private static _instance;
|
11
|
-
constructor(clientKey: string, decryptionKey: string);
|
12
|
-
static getGrowthBookInstance: (clientKey: string, decryptionKey: string) => Growthbook;
|
13
|
-
setAttributes: ({ id, country, user_language, device_language, device_type, utm_source, utm_medium, utm_campaign, is_authorised, }: TGrowthbookAttributes) => void;
|
11
|
+
constructor(clientKey: string, decryptionKey: string, settings?: Partial<Context>);
|
12
|
+
static getGrowthBookInstance: (clientKey: string, decryptionKey: string, growthbookOptions?: Partial<Context>) => Growthbook;
|
13
|
+
setAttributes: ({ id, country, user_language, device_language, device_type, utm_source, utm_medium, utm_campaign, is_authorised, url, }: TGrowthbookAttributes) => void;
|
14
14
|
getFeatureValue: <K extends string | number, V extends GrowthbookConfigs[K]>(key: K, defaultValue: V) => import("@growthbook/growthbook").WidenPrimitives<V>;
|
15
15
|
getFeatureState: (id: string) => import("@growthbook/growthbook").FeatureResult<string | boolean | Record<string, boolean> | [] | null>;
|
16
|
-
setUrl: (href: string) => void
|
16
|
+
setUrl: (href: string) => Promise<void>;
|
17
17
|
isOn: (key: string) => boolean;
|
18
18
|
init: () => Promise<void>;
|
19
19
|
}
|
package/lib/growthbook.js
CHANGED
@@ -39,11 +39,12 @@ var growthbook_1 = require("@growthbook/growthbook");
|
|
39
39
|
var RudderAnalytics = __importStar(require("rudder-sdk-js"));
|
40
40
|
var Growthbook = /** @class */ (function () {
|
41
41
|
// we have to pass settings due the specific framework implementation
|
42
|
-
function Growthbook(clientKey, decryptionKey) {
|
42
|
+
function Growthbook(clientKey, decryptionKey, settings) {
|
43
|
+
if (settings === void 0) { settings = {}; }
|
43
44
|
var _this = this;
|
44
45
|
this.setAttributes = function (_a) {
|
45
|
-
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;
|
46
|
-
_this.GrowthBook.setAttributes(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({ id: id }, (country !== undefined && { country: 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 })));
|
46
|
+
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;
|
47
|
+
_this.GrowthBook.setAttributes(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({ id: id }, (country !== undefined && { country: 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 })));
|
47
48
|
};
|
48
49
|
this.getFeatureValue = function (key, defaultValue) {
|
49
50
|
return _this.GrowthBook.getFeatureValue(key, defaultValue);
|
@@ -52,13 +53,7 @@ var Growthbook = /** @class */ (function () {
|
|
52
53
|
this.setUrl = function (href) { return _this.GrowthBook.setURL(href); };
|
53
54
|
this.isOn = function (key) { return _this.GrowthBook.isOn(key); };
|
54
55
|
this.init = function () { return _this.GrowthBook.loadFeatures().catch(function (err) { return console.error(err); }); };
|
55
|
-
this.GrowthBook = new growthbook_1.GrowthBook({
|
56
|
-
apiHost: 'https://cdn.growthbook.io',
|
57
|
-
clientKey: clientKey,
|
58
|
-
decryptionKey: decryptionKey,
|
59
|
-
subscribeToChanges: true,
|
60
|
-
enableDevMode: window === null || window === void 0 ? void 0 : window.location.hostname.includes('localhost'),
|
61
|
-
trackingCallback: function (experiment, result) {
|
56
|
+
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) {
|
62
57
|
if (window.dataLayer) {
|
63
58
|
window.dataLayer.push({
|
64
59
|
event: 'experiment_viewed',
|
@@ -72,14 +67,13 @@ var Growthbook = /** @class */ (function () {
|
|
72
67
|
experimentId: experiment.key,
|
73
68
|
variationId: result.variationId,
|
74
69
|
});
|
75
|
-
},
|
76
|
-
});
|
70
|
+
} }, settings));
|
77
71
|
this.init();
|
78
72
|
}
|
79
73
|
// for make instance by singleton
|
80
|
-
Growthbook.getGrowthBookInstance = function (clientKey, decryptionKey) {
|
74
|
+
Growthbook.getGrowthBookInstance = function (clientKey, decryptionKey, growthbookOptions) {
|
81
75
|
if (!Growthbook._instance) {
|
82
|
-
Growthbook._instance = new Growthbook(clientKey, decryptionKey);
|
76
|
+
Growthbook._instance = new Growthbook(clientKey, decryptionKey, growthbookOptions);
|
83
77
|
return Growthbook._instance;
|
84
78
|
}
|
85
79
|
return Growthbook._instance;
|
package/lib/types.d.ts
CHANGED
@@ -8,6 +8,7 @@ export type TGrowthbookAttributes = {
|
|
8
8
|
utm_medium?: 'ppc-native' | 'affiliate' | 'common' | string;
|
9
9
|
utm_campaign?: string;
|
10
10
|
is_authorised?: boolean;
|
11
|
+
url?: string;
|
11
12
|
};
|
12
13
|
export type TCoreAttributes = {
|
13
14
|
account_type?: string;
|
@@ -17,7 +18,7 @@ export type TCoreAttributes = {
|
|
17
18
|
} & Partial<TGrowthbookAttributes>;
|
18
19
|
type SignupProvider = 'email' | 'phone' | 'google' | 'facebook' | 'apple';
|
19
20
|
type VirtualSignupForm = {
|
20
|
-
action?: 'open' | 'started' | 'email_confirmation_sent' | 'email_confirmed' | 'signup_continued' | 'country_selection_screen_opened' | 'password_screen_opened' | 'signup_done' | 'signup_flow_error' | 'go_to_login';
|
21
|
+
action?: 'open' | 'started' | 'email_confirmation_sent' | 'email_confirmed' | 'signup_continued' | 'country_selection_screen_opened' | 'password_screen_opened' | 'signup_done' | 'signup_flow_error' | 'go_to_login' | 'signup_modal_open' | 'signup_modal_close';
|
21
22
|
signup_provider?: SignupProvider;
|
22
23
|
form_source?: string;
|
23
24
|
form_name?: string;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@deriv-com/analytics",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.5.0",
|
4
4
|
"description": "The analytics package contains all the utility functions used for tracking user events and sending them to the respective platform such as Rudderstack.",
|
5
5
|
"keywords": [
|
6
6
|
"rudderstack",
|
@@ -36,7 +36,7 @@
|
|
36
36
|
"typescript": "^4.9.5"
|
37
37
|
},
|
38
38
|
"dependencies": {
|
39
|
-
"@growthbook/growthbook": "^0.
|
39
|
+
"@growthbook/growthbook": "^0.36.0",
|
40
40
|
"rudder-sdk-js": "^2.35.0"
|
41
41
|
},
|
42
42
|
"engines": {
|