manifold/web/lib/service/analytics.ts
Barak Gila 7ba19c274b
basic sprig integration with possible page URL events (#932)
* basic sprig integration with possible page URL events

* iteration 0

* iteration 1

* run prettier; attempt to remove expect error

* readd expect error messages

* typescript comment fixes

* add identify

* remove package-lock.json

* extract to separate file

* fix linting

* fix lint

* fix lint

* fix missing config
2022-09-27 12:02:03 -05:00

47 lines
1011 B
TypeScript

import {
init,
track,
identify,
setUserId,
Identify,
} from '@amplitude/analytics-browser'
import * as Sprig from 'web/lib/service/sprig'
import { ENV_CONFIG } from 'common/envs/constants'
init(ENV_CONFIG.amplitudeApiKey ?? '', undefined, { includeReferrer: true })
export { track }
// Convenience functions:
export const trackCallback =
(eventName: string, eventProperties?: any) => () => {
track(eventName, eventProperties)
}
export const withTracking =
(
f: (() => void) | (() => Promise<void>),
eventName: string,
eventProperties?: any
) =>
async () => {
const promise = f()
track(eventName, eventProperties)
await promise
}
export async function identifyUser(userId: string) {
setUserId(userId)
Sprig.setUserId(userId)
}
export async function setUserProperty(property: string, value: string) {
const identifyObj = new Identify()
identifyObj.set(property, value)
await identify(identifyObj)
Sprig.setAttributes({ [property]: value })
}