From 57e6dcc614724332d9584f6e5f8c1bc63a541af9 Mon Sep 17 00:00:00 2001 From: bio-crow Date: Mon, 6 May 2024 19:46:40 +0300 Subject: [PATCH] feat: add async growthbook initialising --- .DS_Store | Bin 6148 -> 6148 bytes package-lock.json | 12 ++++++------ package.json | 2 +- src/analytics.ts | 25 +++++++++++++++++++------ src/growthbook.ts | 12 +++++++----- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/.DS_Store b/.DS_Store index f0e5240ab6504296595858cfc0a40dd1f68461b8..acc2d3e4851325c1aa7a9ccd6b8963fb495ad966 100644 GIT binary patch delta 36 scmZoMXfc@J&&abeU^g=(&t@JLM#jm;ta_VgvgR>OY>3;;&heKY0K=^c-2eap delta 103 zcmZoMXfc@J&&aniU^g=(-)0^bMn-97h8%`Wh9u9N{N$vZ{3Hej1_2;;WME+Ao6N*& osma5T$B@sE!jQ@k&yWjdl>&97G8CifSif0^wU%i!JI7ys0F&z$;Q#;t diff --git a/package-lock.json b/package-lock.json index f94a591..4a05c42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@deriv-com/analytics", - "version": "1.4.13", + "version": "1.5.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@deriv-com/analytics", - "version": "1.4.13", + "version": "1.5.2", "license": "MIT", "dependencies": { - "@growthbook/growthbook": "^0.36.0", + "@growthbook/growthbook": "^1.0.0", "rudder-sdk-js": "^2.35.0" }, "devDependencies": { @@ -673,9 +673,9 @@ "dev": true }, "node_modules/@growthbook/growthbook": { - "version": "0.36.0", - "resolved": "https://registry.npmjs.org/@growthbook/growthbook/-/growthbook-0.36.0.tgz", - "integrity": "sha512-5u1x34H7pg5zS5db3UZ1Pn5hL/jj1EOWdUuz5tSIDUfW49TOWtxtrOAx0Qu1B+UmrIcZKE5XJr6TmWdmdOK12g==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@growthbook/growthbook/-/growthbook-1.0.0.tgz", + "integrity": "sha512-qfVhZcubsGjJI8oNjZp5g69N7c+NMxWl3WS05Sa93V6qNjE9Uz2PZqj7Qj+6iTEOZ1ignmEsIBJEKfdlpa9INg==", "dependencies": { "dom-mutator": "^0.6.0" }, diff --git a/package.json b/package.json index 3263df1..17210df 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "typescript": "^4.9.5" }, "dependencies": { - "@growthbook/growthbook": "^0.36.0", + "@growthbook/growthbook": "^1.0.0", "rudder-sdk-js": "^2.35.0" }, "engines": { diff --git a/src/analytics.ts b/src/analytics.ts index bff9c66..5cd82cd 100644 --- a/src/analytics.ts +++ b/src/analytics.ts @@ -17,15 +17,27 @@ export function createAnalyticsInstance(options?: Options) { tracking_config: { [key: string]: boolean } = {}, offline_cache: { [key: string]: { event: keyof TEvents; payload: TEvents[keyof TEvents] } } = {} - const initialise = ({ growthbookKey, growthbookDecryptionKey, rudderstackKey, growthbookOptions }: Options) => { + const initialise = async ({ + growthbookKey, + growthbookDecryptionKey, + rudderstackKey, + growthbookOptions, + }: Options) => { _rudderstack = RudderStack.getRudderStackInstance(rudderstackKey) if (growthbookKey && growthbookDecryptionKey) { _growthbook = Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions) - let interval = setInterval(() => { - if (Object.keys(tracking_config).length > 0) clearInterval(interval) - else tracking_config = getFeatureValue('tracking-buttons-config', {}) - }, 1000) + // Wait for Growthbook initialization to complete + await new Promise(resolve => { + let interval = setInterval(() => { + if (Object.keys(tracking_config).length > 0) { + clearInterval(interval) + resolve() + } else { + tracking_config = getFeatureValue('tracking-buttons-config', {}) + } + }, 1000) + }) } } @@ -83,7 +95,8 @@ export function createAnalyticsInstance(options?: Options) { /** * Pushes page view event to Rudderstack * - * @param curret_page The name or URL of the current page to track the page view event + * @param current_page The name or URL of the current page to track the page view event + * @param platform */ const pageView = (current_page: string, platform = 'Deriv App') => { if (!_rudderstack) return diff --git a/src/growthbook.ts b/src/growthbook.ts index 5d8bf5f..121074a 100644 --- a/src/growthbook.ts +++ b/src/growthbook.ts @@ -1,4 +1,4 @@ -import { Context, GrowthBook } from '@growthbook/growthbook' +import { Context, GrowthBook, LocalStorageStickyBucketService } from '@growthbook/growthbook' import * as RudderAnalytics from 'rudder-sdk-js' import { TGrowthbookAttributes } from './types' @@ -22,7 +22,8 @@ export class Growthbook { decryptionKey, antiFlicker: false, navigateDelay: 0, - antiFlickerTimeout: 3500, + antiFlickerTimeout: 1000, + stickyBucketService: new LocalStorageStickyBucketService(), subscribeToChanges: true, enableDevMode: window?.location.hostname.includes('localhost'), trackingCallback: (experiment, result) => { @@ -58,7 +59,7 @@ export class Growthbook { return Growthbook._instance } - setAttributes = ({ + setAttributes = async ({ id, country, user_language, @@ -70,7 +71,7 @@ export class Growthbook { is_authorised, url, }: TGrowthbookAttributes) => { - this.GrowthBook.setAttributes({ + await this.GrowthBook.setAttributes({ id, ...(country !== undefined && { country }), ...(user_language !== undefined && { user_language }), @@ -90,5 +91,6 @@ export class Growthbook { setUrl = (href: string) => this.GrowthBook.setURL(href) isOn = (key: string) => this.GrowthBook.isOn(key) - init = () => this.GrowthBook.loadFeatures().catch(err => console.error(err)) + // new one init is async, old one loadFeatures is synced + init = () => this.GrowthBook.init({ timeout: 1000, streaming: true }) }