From 3b75e60bd2a4f2e8a3726f68920fc534f943b5a3 Mon Sep 17 00:00:00 2001 From: Bill Date: Tue, 6 Feb 2024 21:43:05 -0800 Subject: [PATCH] Add custom amplitude server URL (#282) * Add custom amplitude server URL * string * add to readme --- .env.example | 1 + README.md | 1 + scripts/inject-amplitude.js | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index cbfc529..91cebfe 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,7 @@ VITE_V3_TOKEN_ADDRESS= VITE_TOKEN_MIGRATION_URI= AMPLITUDE_API_KEY= +AMPLITUDE_SERVER_URL= BUGSNAG_API_KEY= IOS_APP_ID= INTERCOM_APP_ID= diff --git a/README.md b/README.md index 983ebc4..d7a22b2 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Set environment variables via `.env`. - `VITE_V3_TOKEN_ADDRESS` (optional): Address of the V3 $DYDX token. - `VITE_TOKEN_MIGRATION_URI` (optional): The URL of the token migration website. - `AMPLITUDE_API_KEY` (optional): Amplitude API key for enabling Amplitude; used with `pnpm run build:inject-amplitude`. +- `AMPLITUDE_SERVER_URL` (optional): Custom Amplitude server URL - `BUGSNAG_API_KEY` (optional): API key for enabling Bugsnag; used with `pnpm run build:inject-bugsnag`. - `IOS_APP_ID` (optional): iOS app ID used for enabling deep linking to the iOS app; used with `pnpm run build:inject-app-deeplinks`. - `INTERCOM_APP_ID` (optional): Used for enabling Intercom; utilized with `pnpm run build:inject-intercom`. diff --git a/scripts/inject-amplitude.js b/scripts/inject-amplitude.js index 6848b45..befa1c8 100644 --- a/scripts/inject-amplitude.js +++ b/scripts/inject-amplitude.js @@ -3,12 +3,13 @@ import path from 'path'; import { fileURLToPath } from 'url'; const AMPLITUDE_API_KEY = process.env.AMPLITUDE_API_KEY; +const AMPLITUDE_SERVER_URL = process.env.AMPLITUDE_SERVER_URL; const currentPath = fileURLToPath(import.meta.url); const projectRoot = path.dirname(currentPath); const htmlFilePath = path.resolve(projectRoot, '../dist/index.html'); -if(AMPLITUDE_API_KEY){ +if (AMPLITUDE_API_KEY) { try { const html = await fs.readFile(htmlFilePath, 'utf-8'); @@ -18,8 +19,34 @@ if(AMPLITUDE_API_KEY){ `; const amplitudeListenerScript = ``; + !(function () { + var e = "${AMPLITUDE_API_KEY}"; + e && + (globalThis.amplitude.init(e${ + AMPLITUDE_SERVER_URL + ? `, undefined, { + serverUrl: "${AMPLITUDE_SERVER_URL}" + }` + : '' + }), + globalThis.amplitude.setOptOut(!1), + globalThis.addEventListener("dydx:track", function (e) { + var t = e.detail.eventType, + d = e.detail.eventData; + globalThis.amplitude.track(t, d); + }), + globalThis.addEventListener("dydx:identify", function (e) { + var t = e.detail.property, + d = e.detail.propertyValue; + if ("walletAddress" === t) globalThis.amplitude.setUserId(d); + else { + var i = new globalThis.amplitude.Identify(); + i.set(t, d), globalThis.amplitude.identify(i); + } + }), + console.log("Amplitude enabled.")); + })(); + `; const injectedHtml = html.replace( '
',