Commit 8eef757f authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

refactor: analytics initialization (#4070)

* refactor: analytics initialization

* fix: typings
parent b1c29b3b
...@@ -6,8 +6,8 @@ import { Identify, identify, init, track } from '@amplitude/analytics-browser' ...@@ -6,8 +6,8 @@ import { Identify, identify, init, track } from '@amplitude/analytics-browser'
* Uniswap has two Amplitude projects: test and production. You must be a * Uniswap has two Amplitude projects: test and production. You must be a
* member of the organization on Amplitude to view details. * member of the organization on Amplitude to view details.
*/ */
export function initializeAnalytics() { export function initializeAnalytics(isDevEnvironment = process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV === 'development') return if (isDevEnvironment) return
const API_KEY = process.env.REACT_APP_AMPLITUDE_KEY const API_KEY = process.env.REACT_APP_AMPLITUDE_KEY
if (typeof API_KEY === 'undefined') { if (typeof API_KEY === 'undefined') {
...@@ -25,7 +25,7 @@ export function initializeAnalytics() { ...@@ -25,7 +25,7 @@ export function initializeAnalytics() {
city: false, city: false,
region: false, region: false,
country: false, country: false,
dma: false, // Disables designated market area tracking dma: false, // designated market area
}, },
} }
) )
...@@ -34,7 +34,7 @@ export function initializeAnalytics() { ...@@ -34,7 +34,7 @@ export function initializeAnalytics() {
/** Sends an event to Amplitude. */ /** Sends an event to Amplitude. */
export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<string, unknown>) { export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<string, unknown>) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
console.log('amplitude event log:', `${eventName}: ${JSON.stringify(eventProperties)}`) console.debug(`[amplitude(${eventName})]: ${JSON.stringify(eventProperties)}`)
return return
} }
...@@ -42,68 +42,47 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s ...@@ -42,68 +42,47 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s
} }
/** /**
* Class that exposes methods to modify the User Model's properties in * Class that exposes methods to mutate the User Model's properties in
* Amplitude that represents the current session's user. * Amplitude that represents the current session's user.
* *
* See https://help.amplitude.com/hc/en-us/articles/115002380567-User-properties-and-event-properties * See https://help.amplitude.com/hc/en-us/articles/115002380567-User-properties-and-event-properties
* for details. * for details.
*/ */
class UserModel { class UserModel {
private _isDevEnvironemnt = true constructor(private isDevEnvironment = process.env.NODE_ENV === 'development') {}
constructor() { private log(method: string, ...parameters: unknown[]) {
process.env.NODE_ENV === 'development' ? (this._isDevEnvironemnt = true) : (this._isDevEnvironemnt = false) console.debug(`[amplitude(Identify)]: ${method}(${parameters})`)
} }
public set(propertyName: string, propertyValue: string | number) { private call(mutate: (event: Identify) => Identify) {
if (this._isDevEnvironemnt) { if (this.isDevEnvironment) {
console.log('amplitude user model update with operation set:', `${propertyName}: ${propertyValue}`) const log = (_: Identify, method: string) => this.log.bind(this, method)
mutate(new Proxy(new Identify(), { get: log }))
return return
} }
const identifyObj = new Identify() identify(mutate(new Identify()))
identifyObj.set(propertyName, propertyValue)
identify(identifyObj)
} }
public setOnce(propertyName: string, propertyValue: string | number) { set(key: string, value: string | number) {
if (this._isDevEnvironemnt) { this.call((event) => event.set(key, value))
console.log('amplitude user model update with operation setOnce:', `${propertyName}: ${propertyValue}`)
return
}
const identifyObj = new Identify()
identifyObj.setOnce(propertyName, propertyValue)
identify(identifyObj)
} }
public add(propertyName: string, propertyValue: string | number) { setOnce(key: string, value: string | number) {
if (this._isDevEnvironemnt) { this.call((event) => event.setOnce(key, value))
console.log('amplitude user model update with operation add:', `${propertyName}: ${propertyValue}`)
return
}
const identifyObj = new Identify()
identifyObj.add(propertyName, typeof propertyValue === 'number' ? propertyValue : 0)
identify(identifyObj)
} }
public postInsert(propertyName: string, propertyValue: string | number) { add(key: string, value: string | number) {
if (this._isDevEnvironemnt) { this.call((event) => event.add(key, typeof value === 'number' ? value : 0))
console.log('amplitude user model update with operation postInsert:', `${propertyName}: ${propertyValue}`)
return
}
const identifyObj = new Identify()
identifyObj.postInsert(propertyName, propertyValue)
identify(identifyObj)
} }
public remove(propertyName: string, propertyValue: string | number) { postInsert(key: string, value: string | number) {
if (this._isDevEnvironemnt) { this.call((event) => event.postInsert(key, value))
console.log('amplitude user model update with operation remove:', `${propertyName}: ${propertyValue}`) }
return
} remove(key: string, value: string | number) {
const identifyObj = new Identify() this.call((event) => event.remove(key, value))
identifyObj.remove(propertyName, propertyValue)
identify(identifyObj)
} }
} }
export const userModel = new UserModel() export const user = new UserModel()
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