forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adrienne / Rudderstack integration (binary-com#9013)
* feat: integrated rudderstack hook * feat: added test cases for use-rudderstack hook * refactor: moved rudderstack out of store and into utils * chore: updated conditions * chore: removed rudderstack script html * chore: removed test functions * chore: removed test functions * chore: removed old changes * chore: refactored code based on reviews * chore: test commit * chore: removed package-lock.json * chore: added optional for tab_name * feat: moved analytics into its own package * chore: incorporated code review changes * chore: removed changed files from merge conflict * chore: removed changed files from merge conflict * chore: removed prettier changes * chore: reverted changes from reviews * chore: incorporated code review changes * fix: separated env variables for ruddetstack * refactor: updated conditions --------- Co-authored-by: Ali(Ako) Hosseini <ali.hosseini@deriv.com> Co-authored-by: Jim Daniels Wasswa <104334373+jim-deriv@users.noreply.github.com>
- Loading branch information
1 parent
83022b9
commit 37b843d
Showing
16 changed files
with
282 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const baseConfigForPackages = require('../../jest.config.base'); | ||
|
||
module.exports = { | ||
...baseConfigForPackages, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@deriv/analytics", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"devDependencies": { | ||
"typescript": "^4.6.3" | ||
}, | ||
"dependencies": { | ||
"rudder-sdk-js": "^2.35.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { RudderStack } from '../rudderstack'; | ||
|
||
jest.mock('rudder-sdk-js', () => { | ||
const original_module = jest.requireActual('rudder-sdk-js'); | ||
return { | ||
...original_module, | ||
load: jest.fn(), | ||
ready: (callback: () => any) => callback(), | ||
track: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('rudderstack', () => { | ||
let rudderstack: RudderStack; | ||
const originalEnv = process.env; | ||
|
||
beforeAll(() => { | ||
process.env = { | ||
...originalEnv, | ||
CIRCLE_JOB: 'release_staging', | ||
RUDDERSTACK_PRODUCTION_KEY: '123456789', | ||
RUDDERSTACK_STAGING_KEY: '123456789', | ||
RUDDERSTACK_URL: 'http://example.com', | ||
}; | ||
|
||
rudderstack = new RudderStack(); | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
test('should be initialized when instance is created', () => { | ||
expect(rudderstack.has_initialized).toBe(true); | ||
}); | ||
|
||
test('should be identified once identify event is called', () => { | ||
rudderstack.identifyEvent('C123', { | ||
language: 'en', | ||
}); | ||
|
||
expect(rudderstack.has_identified).toBe(true); | ||
}); | ||
|
||
test('should not be empty if current page is passed', () => { | ||
rudderstack.pageView('app.deriv.com'); | ||
|
||
expect(rudderstack.current_page).not.toBe(''); | ||
}); | ||
|
||
test('should be called once when track is invoked', () => { | ||
const spy = jest.spyOn(rudderstack, 'track'); | ||
rudderstack.track('ce_trade_types_form', { | ||
action: 'open', | ||
}); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should not be identified when reset is called', () => { | ||
rudderstack.reset(); | ||
|
||
expect(rudderstack.has_identified).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as RudderStack } from './rudderstack'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import * as RudderAnalytics from 'rudder-sdk-js'; | ||
|
||
type SignupProvider = 'email' | 'phone' | 'google' | 'facebook' | 'apple'; | ||
|
||
type VirtualSignupFormAction = { | ||
action: | ||
| 'open' | ||
| 'started' | ||
| 'email_confirmed' | ||
| 'signup_continued' | ||
| 'country_selection_screen_opened' | ||
| 'password_screen_opened' | ||
| 'signup_done' | ||
| 'signup_flow_error' | ||
| 'go_to_login'; | ||
signup_provider?: SignupProvider; | ||
form_source?: string; | ||
form_name?: string; | ||
error_message?: string; | ||
}; | ||
|
||
type RealAccountSignupFormAction = { | ||
action: | ||
| 'open' | ||
| 'step_passed' | ||
| 'save' | ||
| 'restore' | ||
| 'close' | ||
| 'real_signup_error' | ||
| 'other_error' | ||
| 'real_signup_finished'; | ||
step_codename?: string; | ||
step_num?: number; | ||
user_choice?: string; | ||
source?: string; | ||
form_name?: string; | ||
real_signup_error_message?: string; | ||
landing_company: string; | ||
}; | ||
|
||
type VirtualSignupEmailConfirmationAction = { | ||
action: 'received' | 'expired' | 'confirmed' | 'error'; | ||
signup_provider?: SignupProvider; | ||
form_source?: string; | ||
email_md5?: string; | ||
error_message?: string; | ||
}; | ||
|
||
type TradeTypesFormAction = | ||
| { | ||
action: 'open' | 'close' | 'info_close'; | ||
trade_type_name?: string; | ||
tab_name?: string; | ||
form_source?: string; | ||
form_name?: string; | ||
subform_name?: string; | ||
} | ||
| { | ||
action: 'choose_trade_type'; | ||
subform_name: 'info_old' | 'info_new'; | ||
form_name: string; | ||
trade_type_name: string; | ||
} | ||
| { | ||
action: 'choose_trade_type'; | ||
subform_name: 'trade_type'; | ||
tab_name: string; | ||
form_name: string; | ||
trade_type_name: string; | ||
} | ||
| { | ||
action: 'search'; | ||
search_string: string; | ||
} | ||
| { | ||
action: 'info_open'; | ||
tab_name: string; | ||
trade_type_name: string; | ||
} | ||
| { | ||
action: 'info-switcher'; | ||
info_switcher_mode: string; | ||
trade_type_name: string; | ||
}; | ||
|
||
type IdentifyAction = { | ||
language: string; | ||
}; | ||
|
||
type TEvents = { | ||
ce_virtual_signup_form: VirtualSignupFormAction; | ||
ce_real_account_signup_form: RealAccountSignupFormAction; | ||
ce_virtual_signup_email_confirmation: VirtualSignupEmailConfirmationAction; | ||
ce_trade_types_form: TradeTypesFormAction; | ||
identify: IdentifyAction; | ||
}; | ||
|
||
export class RudderStack { | ||
has_identified = false; | ||
has_initialized = false; | ||
current_page = ''; | ||
|
||
constructor() { | ||
this.init(); | ||
} | ||
|
||
init() { | ||
const isProduction = process.env.CIRCLE_JOB === 'release_production'; | ||
const isStaging = process.env.CIRCLE_JOB === 'release_staging'; | ||
|
||
let RUDDERSTACK_KEY; | ||
if (isProduction) { | ||
RUDDERSTACK_KEY = process.env.RUDDERSTACK_PRODUCTION_KEY; | ||
} else if (isStaging) { | ||
RUDDERSTACK_KEY = process.env.RUDDERSTACK_STAGING_KEY; | ||
} | ||
|
||
const RUDDERSTACK_URL = process.env.RUDDERSTACK_URL; | ||
if (RUDDERSTACK_KEY && RUDDERSTACK_URL) { | ||
RudderAnalytics.load(RUDDERSTACK_KEY, RUDDERSTACK_URL); | ||
RudderAnalytics.ready(() => { | ||
this.has_initialized = true; | ||
}); | ||
} | ||
} | ||
|
||
identifyEvent = (user_id: string, payload: TEvents['identify']) => { | ||
if (this.has_initialized) { | ||
RudderAnalytics.identify(user_id, payload); | ||
this.has_identified = true; | ||
} | ||
}; | ||
|
||
/** | ||
* Pushes page view track event to rudderstack | ||
*/ | ||
pageView(current_page: string) { | ||
if (this.has_initialized && this.has_identified && current_page !== this.current_page) { | ||
RudderAnalytics.page('Deriv App', current_page); | ||
this.current_page = current_page; | ||
} | ||
} | ||
|
||
/** | ||
* Pushes reset event to rudderstack | ||
*/ | ||
reset() { | ||
if (this.has_initialized) { | ||
RudderAnalytics.reset(); | ||
this.has_identified = false; | ||
} | ||
} | ||
|
||
/** | ||
* Pushes track events to rudderstack | ||
*/ | ||
track<T extends keyof TEvents>(event: T, payload: TEvents[T]) { | ||
if (this.has_initialized && this.has_identified) { | ||
RudderAnalytics.track(event, payload); | ||
} | ||
} | ||
} | ||
|
||
export default new RudderStack(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.