forked from JackHamer09/zksync-plus
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23c74fa
commit 5a0524b
Showing
5 changed files
with
54 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,80 @@ | ||
let analyticsLoaded = false; | ||
import type { Hash } from "@/types"; | ||
|
||
const isRudderReady = (): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
if (!window.rudderanalytics) { | ||
reject(new Error("Rudder not loaded")); | ||
} | ||
resolve(); | ||
}); | ||
}; | ||
let analyticsLoaded = false; | ||
|
||
const isMasaReady = (): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
if (!window.masaAnalytics) { | ||
reject(new Error("Masa not loaded")); | ||
} | ||
async function loadRudder() { | ||
if (!window.rudderanalytics) { | ||
await new Promise((resolve) => setTimeout(resolve, 250)); | ||
throw new Error("Rudder not loaded"); | ||
} | ||
const runtimeConfig = useRuntimeConfig(); | ||
window.rudderanalytics.load( | ||
runtimeConfig.public.analytics.rudder.key, | ||
runtimeConfig.public.analytics.rudder.dataplaneUrl | ||
); | ||
} | ||
|
||
resolve(); | ||
async function loadMasa() { | ||
if (!window.MA) { | ||
await new Promise((resolve) => setTimeout(resolve, 250)); | ||
throw new Error("Masa not loaded"); | ||
} | ||
const runtimeConfig = useRuntimeConfig(); | ||
window.masaAnalytics = new window.MA.MasaAnalytics({ | ||
clientId: runtimeConfig.public.analytics.masa.clientId, | ||
}); | ||
}; | ||
} | ||
|
||
export async function initAnalytics(): Promise<boolean> { | ||
if (analyticsLoaded) return true; | ||
|
||
const runtimeConfig = useRuntimeConfig(); | ||
const useRudder = Boolean(runtimeConfig.public.rudderKey && runtimeConfig.public.dataplaneUrl); | ||
const useMasa = Boolean(runtimeConfig.public.masaKey); | ||
if (!useRudder && !useMasa) { | ||
const useRudder = Boolean( | ||
runtimeConfig.public.analytics.rudder.key && runtimeConfig.public.analytics.rudder.dataplaneUrl | ||
); | ||
const useMasa = Boolean(runtimeConfig.public.analytics.masa.clientId); | ||
if ((!useRudder && !useMasa) || analyticsLoaded) { | ||
return false; | ||
} | ||
|
||
if (analyticsLoaded) { | ||
await Promise.all([await isRudderReady(), await isMasaReady()]); | ||
return true; | ||
} | ||
const rudderWait = retry(async () => { | ||
if (useRudder && !window.rudderanalytics) { | ||
await new Promise((resolve) => setTimeout(resolve, 250)); | ||
throw new Error("Rudder not loaded"); | ||
} | ||
}); | ||
const masaWait = retry(async () => { | ||
if (useMasa && !window.MA) { | ||
await new Promise((resolve) => setTimeout(resolve, 250)); | ||
throw new Error("Rudder not loaded"); | ||
} | ||
}); | ||
await Promise.all([await rudderWait, await masaWait]); | ||
useRudder && window.rudderanalytics?.load(runtimeConfig.public.rudderKey, runtimeConfig.public.dataplaneUrl); | ||
if (useMasa && window.MA) { | ||
window.masaAnalytics = new window.MA!.MasaAnalytics({ | ||
clientId: runtimeConfig.public.masaKey, | ||
}); | ||
} | ||
analyticsLoaded = true; | ||
const services = []; | ||
if (useRudder) services.push(loadRudder()); | ||
if (useMasa) services.push(loadMasa()); | ||
|
||
await Promise.all([await isRudderReady(), await isMasaReady()]); | ||
await Promise.all(services); | ||
analyticsLoaded = true; | ||
return true; | ||
} | ||
|
||
export async function trackPage(): Promise<void> { | ||
if (await initAnalytics()) { | ||
const runtimeConfig = useRuntimeConfig(); | ||
window.rudderanalytics?.page(); | ||
window.masaAnalytics?.firePageViewEvent({ | ||
page: window.location.href, | ||
additionalEventData: { appId: "zksync-portal" }, | ||
additionalEventData: { appId: runtimeConfig.public.analytics.masa.appId }, | ||
}); | ||
} | ||
} | ||
|
||
export async function trackEvent(eventName: string, params?: object): Promise<void> { | ||
if (await initAnalytics()) { | ||
const runtimeConfig = useRuntimeConfig(); | ||
window.rudderanalytics?.track(eventName, params); | ||
window.masaAnalytics?.trackCustomEvent({ eventName, additionalEventData: { appId: "zksync-portal", ...params } }); | ||
window.masaAnalytics?.trackCustomEvent({ | ||
eventName, | ||
additionalEventData: { appId: runtimeConfig.public.analytics.masa.appId, ...params }, | ||
}); | ||
} | ||
} | ||
|
||
export async function identifyWallet(userAddress: `0x${string}` | undefined, walletType?: string): Promise<void> { | ||
export async function identifyWallet(userAddress: Hash | undefined, walletType?: string): Promise<void> { | ||
if (await initAnalytics()) { | ||
const runtimeConfig = useRuntimeConfig(); | ||
window.masaAnalytics?.fireConnectWalletEvent({ | ||
user_address: userAddress, | ||
wallet_type: walletType, | ||
additionalEventData: { appId: "zksync-portal" }, | ||
additionalEventData: { appId: runtimeConfig.public.analytics.masa.appId }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters