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.
Merge branch 'main' into feat/integrate-wsteth-bridge
- Loading branch information
Showing
8 changed files
with
111 additions
and
26 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
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,46 +1,80 @@ | ||
import type { Hash } from "@/types"; | ||
|
||
let analyticsLoaded = false; | ||
|
||
const isReady = (): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
if (!window.rudderanalytics) { | ||
reject(new Error("Rudder not loaded")); | ||
} | ||
window.rudderanalytics?.ready(() => { | ||
resolve(); | ||
}); | ||
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 | ||
); | ||
} | ||
|
||
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(); | ||
if (!runtimeConfig.public.rudderKey || !runtimeConfig.public.dataplaneUrl) { | ||
const useRudder = Boolean( | ||
runtimeConfig.public.analytics.rudder.key && runtimeConfig.public.analytics.rudder.dataplaneUrl | ||
); | ||
const useMasa = Boolean(runtimeConfig.public.analytics.masa.clientId && runtimeConfig.public.analytics.masa.appId); | ||
if ((!useRudder && !useMasa) || analyticsLoaded) { | ||
return false; | ||
} | ||
|
||
if (analyticsLoaded) { | ||
await isReady(); | ||
return true; | ||
} | ||
await retry(async () => { | ||
if (!window.rudderanalytics) { | ||
await new Promise((resolve) => setTimeout(resolve, 250)); | ||
throw new Error("Rudder not loaded"); | ||
} | ||
}); | ||
window.rudderanalytics?.load(runtimeConfig.public.rudderKey, runtimeConfig.public.dataplaneUrl); | ||
const services = []; | ||
if (useRudder) services.push(loadRudder()); | ||
if (useMasa) services.push(loadMasa()); | ||
|
||
await Promise.all(services); | ||
analyticsLoaded = true; | ||
await isReady(); | ||
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: runtimeConfig.public.analytics.masa.appId }, | ||
}); | ||
} | ||
} | ||
|
||
export async function trackEvent(eventName: string, params?: unknown): Promise<void> { | ||
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: runtimeConfig.public.analytics.masa.appId, ...params }, | ||
}); | ||
} | ||
} | ||
|
||
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: 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