From 7ba19c274bb28cfb6116bdc453f830b003916750 Mon Sep 17 00:00:00 2001 From: Barak Gila Date: Tue, 27 Sep 2022 10:02:03 -0700 Subject: [PATCH] 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 --- common/envs/dev.ts | 1 + common/envs/prod.ts | 2 ++ web/lib/service/analytics.ts | 4 ++++ web/lib/service/sprig.ts | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 web/lib/service/sprig.ts diff --git a/common/envs/dev.ts b/common/envs/dev.ts index 96ec4dc2..ff3fd37d 100644 --- a/common/envs/dev.ts +++ b/common/envs/dev.ts @@ -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', } diff --git a/common/envs/prod.ts b/common/envs/prod.ts index 3014f4e3..d0469d84 100644 --- a/common/envs/prod.ts +++ b/common/envs/prod.ts @@ -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', diff --git a/web/lib/service/analytics.ts b/web/lib/service/analytics.ts index 3ac58055..780d9ba4 100644 --- a/web/lib/service/analytics.ts +++ b/web/lib/service/analytics.ts @@ -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 }) } diff --git a/web/lib/service/sprig.ts b/web/lib/service/sprig.ts new file mode 100644 index 00000000..ee6052b7 --- /dev/null +++ b/web/lib/service/sprig.ts @@ -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): void { + window.Sprig.setAttributes(attributes) +}