Commit 99ef9366 authored by lynn's avatar lynn Committed by GitHub

feat: amplitude production sdk changes (#4312)

* init

* error change

* use isProduction vs isDevelopment to include vercel
parent 235ee5df
REACT_APP_AMPLITUDE_KEY="1c694b28cd089acc2c386d518f93a775" REACT_APP_AMPLITUDE_KEY="b8f7dabddb1c3b03b394619767972160"
REACT_APP_AMPLITUDE_TEST_KEY="1c694b28cd089acc2c386d518f93a775"
REACT_APP_INFURA_KEY="099fc58e0de9451d80b18d7c74caa7c1" REACT_APP_INFURA_KEY="099fc58e0de9451d80b18d7c74caa7c1"
REACT_APP_FORTMATIC_KEY="pk_live_F937DF033A1666BF" REACT_APP_FORTMATIC_KEY="pk_live_F937DF033A1666BF"
REACT_APP_GOOGLE_ANALYTICS_ID="G-KDP9B6W4H8" REACT_APP_GOOGLE_ANALYTICS_ID="G-KDP9B6W4H8"
......
import { Identify, identify, init, track } from '@amplitude/analytics-browser' 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. * Initializes Amplitude with API key for project.
...@@ -8,11 +8,11 @@ import { isDevelopmentEnv } from 'utils/env' ...@@ -8,11 +8,11 @@ import { isDevelopmentEnv } from 'utils/env'
* member of the organization on Amplitude to view details. * member of the organization on Amplitude to view details.
*/ */
export function initializeAnalytics() { 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') { 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( init(
...@@ -35,9 +35,9 @@ export function initializeAnalytics() { ...@@ -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>) { export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<string, unknown>) {
if (isDevelopmentEnv()) { if (!isProductionEnv()) {
console.log(`[amplitude(${eventName})]: ${JSON.stringify(eventProperties)}`) console.log(`[amplitude(${eventName})]: ${JSON.stringify(eventProperties)}`)
return return
} }
...@@ -45,6 +45,13 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s ...@@ -45,6 +45,13 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s
track(eventName, eventProperties) 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[] type Value = string | number | boolean | string[] | number[]
/** /**
...@@ -60,7 +67,7 @@ class UserModel { ...@@ -60,7 +67,7 @@ class UserModel {
} }
private call(mutate: (event: Identify) => Identify) { private call(mutate: (event: Identify) => Identify) {
if (isDevelopmentEnv()) { if (!isProductionEnv()) {
const log = (_: Identify, method: string) => this.log.bind(this, method) const log = (_: Identify, method: string) => this.log.bind(this, method)
mutate(new Proxy(new Identify(), { get: log })) mutate(new Proxy(new Identify(), { get: log }))
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment