11import * as amplitude from '@amplitude/analytics-browser'
22
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'
84
9- import { EventTracker } from '../events'
10- import { Event } from '../types'
5+ import { Event , EventContext , EventTracker , Identity } from '../types'
116
127const AMPLITUDE_API_KEY = process . env . REACT_APP_AMPLITUDE_API_KEY
138
149export function initAmplitude ( ) {
1510 if ( ! AMPLITUDE_API_KEY ) {
16- return
11+ throw new Error (
12+ 'AMPLITUDE_API_KEY is not defined. Amplitude events will not be tracked.'
13+ )
1714 }
1815 amplitude . init ( AMPLITUDE_API_KEY , {
1916 // Disable all autocapture - may change this in the future
@@ -23,40 +20,21 @@ export function initAmplitude() {
2320}
2421
2522export class AmplitudeEventTracker implements EventTracker {
26- #provider?: InternalProvider
27- #owner?: string
28- #providerOwner?: string
29- #repo?: string
23+ context : EventContext = { }
24+ identity ?: Identity
3025
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
3630 }
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- }
4531
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 ( ) )
5433 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 ) )
5935 amplitude . identify ( identifyEvent )
36+
37+ this . identity = identity
6038 }
6139
6240 track ( event : Event ) {
@@ -65,23 +43,19 @@ export class AmplitudeEventTracker implements EventTracker {
6543 event_type : event . type ,
6644 // eslint-disable-next-line camelcase
6745 event_properties : {
68- owner : this . #providerOwner,
69- repo : this . #repo,
7046 ...event . properties ,
47+ ...this . context ,
7148 } ,
7249 // This attaches the event to the owner's user group as well
73- groups : this . #providerOwner
50+ groups : this . context . owner ?. id
7451 ? {
75- owner : this . #providerOwner ,
52+ owner : this . context . owner . id ,
7653 }
7754 : undefined ,
7855 } )
7956 }
80- }
8157
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+ }
8761}
0 commit comments