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
This commit is contained in:
Barak Gila 2022-09-27 10:02:03 -07:00 committed by GitHub
parent a12ed78813
commit 7ba19c274b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View File

@ -18,4 +18,5 @@ export const DEV_CONFIG: EnvConfig = {
amplitudeApiKey: 'fd8cbfd964b9a205b8678a39faae71b3',
// this is Phil's deployment
twitchBotEndpoint: 'https://king-prawn-app-5btyw.ondigitalocean.app',
sprigEnvironmentId: 'Tu7kRZPm7daP',
}

View File

@ -3,6 +3,7 @@ export type EnvConfig = {
firebaseConfig: FirebaseConfig
amplitudeApiKey?: string
twitchBotEndpoint?: string
sprigEnvironmentId?: string
// IDs for v2 cloud functions -- find these by deploying a cloud function and
// examining the URL, https://[name]-[cloudRunId]-[cloudRunRegion].a.run.app
@ -56,6 +57,7 @@ type FirebaseConfig = {
export const PROD_CONFIG: EnvConfig = {
domain: 'manifold.markets',
amplitudeApiKey: '2d6509fd4185ebb8be29709842752a15',
sprigEnvironmentId: 'sQcrq9TDqkib',
firebaseConfig: {
apiKey: 'AIzaSyDp3J57vLeAZCzxLD-vcPaGIkAmBoGOSYw',

View File

@ -6,6 +6,8 @@ import {
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 })
@ -33,10 +35,12 @@ export const withTracking =
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 })
}

33
web/lib/service/sprig.ts Normal file
View File

@ -0,0 +1,33 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
// Integrate Sprig
import { ENV_CONFIG } from 'common/envs/constants'
try {
;(function (l, e, a, p) {
if (window.Sprig) return
window.Sprig = function (...args) {
S._queue.push(args)
}
const S = window.Sprig
S.appId = a
S._queue = []
window.UserLeap = S
a = l.createElement('script')
a.async = 1
a.src = e + '?id=' + S.appId
p = l.getElementsByTagName('script')[0]
p.parentNode.insertBefore(a, p)
})(document, 'https://cdn.sprig.com/shim.js', ENV_CONFIG.sprigEnvironmentId)
} catch (error) {
console.log('Error initializing Sprig, please complain to Barak', error)
}
export function setUserId(userId: string): void {
window.Sprig.setUserId(userId)
}
export function setAttributes(attributes: Record<string, unknown>): void {
window.Sprig.setAttributes(attributes)
}