Skip to content

Commit 9039164

Browse files
committed
Move getProviderFromUrl and trackOAuthCompletion to analytics-providers
Signed-off-by: Alexander Platov <[email protected]>
1 parent 6847181 commit 9039164

File tree

7 files changed

+50
-47
lines changed

7 files changed

+50
-47
lines changed

packages/analytics-providers/src/utils.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@
1616
import { UAParser } from 'ua-parser-js'
1717
import { getMetadata } from '@hcengineering/platform'
1818
import presentation from '@hcengineering/presentation'
19-
import { desktopPlatform } from '@hcengineering/ui'
19+
import { desktopPlatform, getCurrentLocation } from '@hcengineering/ui'
20+
import { Analytics } from '@hcengineering/analytics'
2021

2122
const parser = UAParser()
2223

24+
let _isSignUp: boolean = false
25+
export const signupStore = {
26+
setSignUpFlow: (isSignUp: boolean) => {
27+
_isSignUp = isSignUp
28+
},
29+
getSignUpFlow: () => {
30+
return _isSignUp
31+
}
32+
}
33+
2334
function getUrlTrackingParams (): Record<string, string | null> {
2435
const params = new URLSearchParams(window.location.search)
2536
return {
@@ -102,3 +113,32 @@ export function collectEventMetadata (properties: Record<string, any> = {}): Rec
102113
...trackingParams
103114
}
104115
}
116+
117+
function getProviderFromUrl (): string | null {
118+
const location = getCurrentLocation()
119+
const referrer = document.referrer
120+
121+
if (referrer.includes('accounts.google.com')) {
122+
return 'google'
123+
} else if (referrer.includes('github.com')) {
124+
return 'github'
125+
} else if (location.query?.provider != null && location.query.provider !== '') {
126+
return location.query.provider
127+
}
128+
129+
return null
130+
}
131+
132+
export function trackOAuthCompletion (result: any): void {
133+
const provider = getProviderFromUrl()
134+
if (provider == null) return
135+
136+
const isSignUp = signupStore.getSignUpFlow()
137+
const success = result != null
138+
139+
const eventPrefix = isSignUp ? 'signup' : 'login'
140+
const eventSuffix = success ? 'completed' : 'error'
141+
const eventName: string = `${eventPrefix}.${provider}.${eventSuffix}`
142+
143+
Analytics.handleEvent(eventName)
144+
}

packages/analytics/src/index.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44

55
import { addEventListener, PlatformEvent, Severity, Status, translate } from '@hcengineering/platform'
66

7-
let _isSignUp: boolean = false
8-
export const signupStore = {
9-
setSignUpFlow: (isSignUp: boolean) => {
10-
_isSignUp = isSignUp
11-
},
12-
getSignUpFlow: () => {
13-
return _isSignUp
14-
}
15-
}
16-
177
export const providers: AnalyticProvider[] = []
188
export interface AnalyticProvider {
199
init: (config: Record<string, any>) => boolean
@@ -86,19 +76,6 @@ export const Analytics = {
8676
}
8777
}
8878

89-
export function trackOAuthCompletion (result: any, provider?: string | null): void {
90-
if (provider == null) return
91-
92-
const isSignUp = signupStore.getSignUpFlow()
93-
const success = result != null
94-
95-
const eventPrefix = isSignUp ? 'signup' : 'login'
96-
const eventSuffix = success ? 'completed' : 'error'
97-
const eventName: string = `${eventPrefix}.${provider}.${eventSuffix}`
98-
99-
Analytics.handleEvent(eventName)
100-
}
101-
10279
addEventListener(PlatformEvent, async (_event, _status: Status) => {
10380
if (_status.severity === Severity.ERROR) {
10481
const label = await translate(_status.code, _status.params, 'en')

plugins/login-resources/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@hcengineering/setting": "^0.6.17",
5050
"@hcengineering/theme": "^0.6.5",
5151
"@hcengineering/analytics": "^0.6.0",
52-
"@hcengineering/account-client": "^0.6.0"
52+
"@hcengineering/account-client": "^0.6.0",
53+
"@hcengineering/analytics-providers": "^0.6.0"
5354
}
5455
}

plugins/login-resources/src/components/Auth.svelte

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
2-
import { Loading, getCurrentLocation } from '@hcengineering/ui'
2+
import { Loading } from '@hcengineering/ui'
33
import { logIn } from '@hcengineering/workbench'
4-
import { trackOAuthCompletion } from '@hcengineering/analytics'
4+
import { trackOAuthCompletion } from '@hcengineering/analytics-providers'
55
import { onMount } from 'svelte'
66
77
import {
@@ -13,21 +13,6 @@
1313
navigateToWorkspace
1414
} from '../utils'
1515
16-
function getProviderFromUrl (): string | null {
17-
const location = getCurrentLocation()
18-
const referrer = document.referrer
19-
20-
if (referrer.includes('accounts.google.com')) {
21-
return 'google'
22-
} else if (referrer.includes('github.com')) {
23-
return 'github'
24-
} else if (location.query?.provider != null && location.query.provider !== '') {
25-
return location.query.provider
26-
}
27-
28-
return null
29-
}
30-
3116
onMount(async () => {
3217
const autoJoinInfo = getAutoJoinInfo()
3318
if (autoJoinInfo != null) {
@@ -36,9 +21,8 @@
3621
}
3722
3823
const result = await getLoginInfoFromQuery()
39-
const provider = getProviderFromUrl()
4024
41-
trackOAuthCompletion(result, provider)
25+
trackOAuthCompletion(result)
4226
4327
if (result != null) {
4428
await logIn(result)

plugins/login-resources/src/components/Join.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import { checkJoined, join, setLoginInfo, signUpJoin } from '../utils'
2020
import Form from './Form.svelte'
2121
22-
import { Analytics, signupStore } from '@hcengineering/analytics'
22+
import { Analytics } from '@hcengineering/analytics'
23+
import { signupStore } from '@hcengineering/analytics-providers'
2324
import { logIn, workbenchId } from '@hcengineering/workbench'
2425
import { onMount } from 'svelte'
2526
import { loginAction, recoveryAction } from '../actions'

plugins/login-resources/src/components/LoginForm.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
-->
1616
<script lang="ts">
1717
import { type IntlString, Severity, Status } from '@hcengineering/platform'
18-
import { signupStore } from '@hcengineering/analytics'
18+
import { signupStore } from '@hcengineering/analytics-providers'
1919
import { onMount } from 'svelte'
2020
2121
import { type BottomAction, doLoginAsGuest, doLoginNavigate, LoginMethods } from '../index'

plugins/login-resources/src/components/SignupForm.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<script lang="ts">
1717
import { OK, Severity, Status } from '@hcengineering/platform'
1818
import { logIn } from '@hcengineering/workbench'
19-
import { signupStore } from '@hcengineering/analytics'
19+
import { signupStore } from '@hcengineering/analytics-providers'
2020
2121
import BottomActionComponent from './BottomAction.svelte'
2222
import login from '../plugin'

0 commit comments

Comments
 (0)