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.
feat: adding analytics with basic page view (#154)
Co-authored-by: Jack Hamer <[email protected]>
- Loading branch information
1 parent
18bebc8
commit ef2d1e9
Showing
7 changed files
with
80 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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(); | ||
}); | ||
}); | ||
}; | ||
|
||
export async function initAnalytics(): Promise<boolean> { | ||
const runtimeConfig = useRuntimeConfig(); | ||
if (!runtimeConfig.public.rudderKey || !runtimeConfig.public.dataplaneUrl) { | ||
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); | ||
analyticsLoaded = true; | ||
await isReady(); | ||
return true; | ||
} | ||
|
||
export async function trackPage(): Promise<void> { | ||
if (await initAnalytics()) { | ||
window.rudderanalytics?.page(); | ||
} | ||
} | ||
|
||
export async function trackEvent(eventName: string, params?: unknown): Promise<void> { | ||
if (await initAnalytics()) { | ||
window.rudderanalytics?.track(eventName, params); | ||
} | ||
} |