1
1
import * as amplitude from '@amplitude/analytics-browser'
2
2
3
- import { Provider } from 'shared/api/helpers'
4
- import {
5
- InternalProvider ,
6
- providerToInternalProvider ,
7
- } from 'shared/utils/provider'
3
+ import { providerToInternalProvider } from 'shared/utils/provider'
8
4
9
- import { EventTracker } from '../events'
10
- import { Event } from '../types'
5
+ import { Event , EventContext , EventTracker , Identity } from '../types'
11
6
12
7
const AMPLITUDE_API_KEY = process . env . REACT_APP_AMPLITUDE_API_KEY
13
8
14
9
export function initAmplitude ( ) {
15
10
if ( ! AMPLITUDE_API_KEY ) {
16
- return
11
+ throw new Error (
12
+ 'AMPLITUDE_API_KEY is not defined. Amplitude events will not be tracked.'
13
+ )
17
14
}
18
15
amplitude . init ( AMPLITUDE_API_KEY , {
19
16
// Disable all autocapture - may change this in the future
@@ -23,40 +20,21 @@ export function initAmplitude() {
23
20
}
24
21
25
22
export class AmplitudeEventTracker implements EventTracker {
26
- #provider?: InternalProvider
27
- #owner?: string
28
- #providerOwner?: string
29
- #repo?: string
23
+ context : EventContext = { }
24
+ identity ?: Identity
30
25
31
- constructor ( provider ?: Provider , owner ?: string , repo ?: string ) {
32
- if ( ! AMPLITUDE_API_KEY ) {
33
- throw new Error (
34
- 'AMPLITUDE_API_KEY is not defined. Ensure the environment variable is defined before attempting to initialize AmplitudeEventTracker.'
35
- )
26
+ identify ( identity : Identity ) {
27
+ if ( JSON . stringify ( this . identity ) === JSON . stringify ( identity ) ) {
28
+ // Don't identify this user again this session.
29
+ return
36
30
}
37
- this . #provider = provider ? providerToInternalProvider ( provider ) : undefined
38
- this . #owner = owner
39
- this . #providerOwner =
40
- this . #provider && this . #owner
41
- ? formatProviderOwner ( this . #provider, this . #owner)
42
- : undefined
43
- this . #repo = repo
44
- }
45
31
46
- identify ( {
47
- userOwnerId,
48
- username,
49
- } : {
50
- userOwnerId : number
51
- username : string
52
- } ) {
53
- amplitude . setUserId ( userOwnerId . toString ( ) )
32
+ amplitude . setUserId ( identity . userOwnerId . toString ( ) )
54
33
const identifyEvent = new amplitude . Identify ( )
55
- if ( this . #provider) {
56
- identifyEvent . set ( 'provider' , this . #provider)
57
- }
58
- identifyEvent . set ( 'username' , username )
34
+ identifyEvent . set ( 'provider' , providerToInternalProvider ( identity . provider ) )
59
35
amplitude . identify ( identifyEvent )
36
+
37
+ this . identity = identity
60
38
}
61
39
62
40
track ( event : Event ) {
@@ -65,23 +43,19 @@ export class AmplitudeEventTracker implements EventTracker {
65
43
event_type : event . type ,
66
44
// eslint-disable-next-line camelcase
67
45
event_properties : {
68
- owner : this . #providerOwner,
69
- repo : this . #repo,
70
46
...event . properties ,
47
+ ...this . context ,
71
48
} ,
72
49
// This attaches the event to the owner's user group as well
73
- groups : this . #providerOwner
50
+ groups : this . context . owner ?. id
74
51
? {
75
- owner : this . #providerOwner ,
52
+ owner : this . context . owner . id ,
76
53
}
77
54
: undefined ,
78
55
} )
79
56
}
80
- }
81
57
82
- function formatProviderOwner ( provider : InternalProvider , ownerName : string ) {
83
- // Helper function to format the owner group name.
84
- // The reason for this is owner names are not unique on their own, but
85
- // provider/owner names are.
86
- return `${ provider } /${ ownerName } `
58
+ setContext ( context : EventContext ) {
59
+ this . context = context
60
+ }
87
61
}
0 commit comments