feat: amplitude production sdk changes (#4312)

* init

* error change

* use isProduction vs isDevelopment to include vercel
This commit is contained in:
lynn 2022-08-15 12:24:31 -04:00 committed by GitHub
parent 235ee5dff9
commit 99ef9366d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

@ -1,4 +1,5 @@
REACT_APP_AMPLITUDE_KEY="1c694b28cd089acc2c386d518f93a775"
REACT_APP_AMPLITUDE_KEY="b8f7dabddb1c3b03b394619767972160"
REACT_APP_AMPLITUDE_TEST_KEY="1c694b28cd089acc2c386d518f93a775"
REACT_APP_INFURA_KEY="099fc58e0de9451d80b18d7c74caa7c1"
REACT_APP_FORTMATIC_KEY="pk_live_F937DF033A1666BF"
REACT_APP_GOOGLE_ANALYTICS_ID="G-KDP9B6W4H8"

@ -1,5 +1,5 @@
import { Identify, identify, init, track } from '@amplitude/analytics-browser'
import { isDevelopmentEnv } from 'utils/env'
import { isProductionEnv } from 'utils/env'
/**
* Initializes Amplitude with API key for project.
@ -8,11 +8,11 @@ import { isDevelopmentEnv } from 'utils/env'
* member of the organization on Amplitude to view details.
*/
export function initializeAnalytics() {
if (isDevelopmentEnv()) return
const API_KEY = isProductionEnv() ? process.env.REACT_APP_AMPLITUDE_KEY : process.env.REACT_APP_AMPLITUDE_TEST_KEY
const API_KEY = process.env.REACT_APP_AMPLITUDE_KEY
if (typeof API_KEY === 'undefined') {
throw new Error(`REACT_APP_AMPLITUDE_KEY must be a defined environment variable`)
const keyName = isProductionEnv() ? 'REACT_APP_AMPLITUDE_KEY' : 'REACT_APP_AMPLITUDE_TEST_KEY'
throw new Error(`${keyName} must be a defined environment variable`)
}
init(
@ -35,9 +35,9 @@ export function initializeAnalytics() {
)
}
/** Sends an event to Amplitude. */
/** Sends an approved (finalized) event to Amplitude production project. */
export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<string, unknown>) {
if (isDevelopmentEnv()) {
if (!isProductionEnv()) {
console.log(`[amplitude(${eventName})]: ${JSON.stringify(eventProperties)}`)
return
}
@ -45,6 +45,13 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s
track(eventName, eventProperties)
}
/** Sends a draft event to Amplitude test project. */
export function sendTestAnalyticsEvent(eventName: string, eventProperties?: Record<string, unknown>) {
if (isProductionEnv()) return
track(eventName, eventProperties)
}
type Value = string | number | boolean | string[] | number[]
/**
@ -60,7 +67,7 @@ class UserModel {
}
private call(mutate: (event: Identify) => Identify) {
if (isDevelopmentEnv()) {
if (!isProductionEnv()) {
const log = (_: Identify, method: string) => this.log.bind(this, method)
mutate(new Proxy(new Identify(), { get: log }))
return