-
Notifications
You must be signed in to change notification settings - Fork 26
feat: instrument the delegation funnel with Vercel Web Analytics #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ECWireless
merged 8 commits into
livepeer:main
from
Jipperism:feat/722-Implement-Instrumentation-Plan
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
342de40
feat: instrument the delegation funnel with Vercel Web Analytics
Jipperism f91ccde
fix: track delegation transaction result from the receipt
Jipperism 8231aed
Merge branch 'main' into feat/722-Implement-Instrumentation-Plan
ECWireless ca8ec6d
fix: only send out delegation_form_started when wallet is connected, …
Jipperism e768dab
fix: only emit transaction confirmed or failed when transaction recei…
Jipperism 75bd299
Merge branch 'main' into feat/722-Implement-Instrumentation-Plan
Jipperism 2681b7e
fix: useHandleTransaction tests
Jipperism 95b6ff2
fix(useHandleTransaction): ensure proper tracking of Safe transaction…
Jipperism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
|
Jipperism marked this conversation as resolved.
|
This file contains hidden or 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,171 @@ | ||
| import { type BeforeSend, track } from "@vercel/analytics"; | ||
| import type { InputData, TransactionIdentifier } from "hooks/useExplorerStore"; | ||
| import type { Config } from "wagmi"; | ||
| import { getPublicClient } from "wagmi/actions"; | ||
|
|
||
| /** | ||
| * Replaces wallet addresses in tracked URLs (e.g. `/accounts/0x…`), so | ||
| * pageviews and events can't be tied to a visitor's wallet. | ||
| */ | ||
| export const redactAddresses: BeforeSend = (event) => ({ | ||
| ...event, | ||
| url: event.url.replace(/0x[0-9a-fA-F]{40}/g, "[address]"), | ||
| }); | ||
|
|
||
| export type DelegationFunnelEvent = | ||
| | "orchestrators_nav_clicked" | ||
| | "orchestrators_page_viewed" | ||
| | "orchestrator_detail_viewed" | ||
| | "wallet_connected" | ||
| | "delegation_form_started" | ||
| | "delegation_transaction_submitted" | ||
| | "delegation_transaction_confirmed" | ||
| | "delegation_transaction_failed" | ||
| | "account_delegating_tab_viewed" | ||
| | "redelegation_started" | ||
| | "unbonding_or_exit_started"; | ||
|
|
||
| export const trackVercelAnalyticsEvent = ( | ||
| event: DelegationFunnelEvent, | ||
| properties?: Parameters<typeof track>[1] | ||
| ) => { | ||
| if (typeof window === "undefined") { | ||
| return; | ||
| } | ||
|
|
||
| track(event, properties); | ||
| }; | ||
|
|
||
| const NON_DELEGATION_PATHS = ["/migrate", "/voting", "/treasury"]; | ||
|
|
||
| /** Coarse entry point a wallet was connected from; never identifying. */ | ||
| const getWalletConnectSurface = (path: string) => { | ||
| if (path === "/") { | ||
| return "home"; | ||
| } | ||
| if (path.startsWith("/orchestrators")) { | ||
| return "orchestrators_list"; | ||
| } | ||
| if (path.startsWith("/accounts/")) { | ||
| return path.includes("/orchestrating") ? "orchestrator_detail" : "account"; | ||
| } | ||
| return "other"; | ||
| }; | ||
|
|
||
| /** | ||
| * Tracks a wallet connect as delegation intent, tagged with the page it was | ||
| * made from. Migration and governance need a wallet for their own reasons, | ||
| * so connects there aren't tracked at all. | ||
| */ | ||
| export const trackWalletConnected = (asPath: string) => { | ||
| const path = asPath.split("?")[0]; | ||
|
|
||
| if ( | ||
| NON_DELEGATION_PATHS.some((nonDelegation) => path.startsWith(nonDelegation)) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| trackVercelAnalyticsEvent("wallet_connected", { | ||
| surface: getWalletConnectSurface(path), | ||
| }); | ||
|
ECWireless marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const trackedOnce = new Set<string>(); | ||
|
|
||
| /** | ||
| * Tracks `event` at most once per page session for `key`, e.g. once per | ||
| * orchestrator no matter how often its pages are revisited. | ||
| */ | ||
| export const trackVercelAnalyticsEventOnce = ( | ||
| event: DelegationFunnelEvent, | ||
| key: string | ||
| ) => { | ||
| const id = `${event}:${key.toLowerCase()}`; | ||
|
|
||
| if (!trackedOnce.has(id)) { | ||
| trackedOnce.add(id); | ||
| trackVercelAnalyticsEvent(event); | ||
| } | ||
| }; | ||
|
|
||
| type TransactionEvents = { | ||
| submitted: DelegationFunnelEvent; | ||
| confirmed?: DelegationFunnelEvent; | ||
| failed?: DelegationFunnelEvent; | ||
| }; | ||
|
|
||
| const REDELEGATION_EVENTS: TransactionEvents = { | ||
| submitted: "redelegation_started", | ||
| }; | ||
|
|
||
| const DELEGATION_EVENTS: Partial< | ||
| Record<TransactionIdentifier, TransactionEvents> | ||
| > = { | ||
| bond: { | ||
| submitted: "delegation_transaction_submitted", | ||
| confirmed: "delegation_transaction_confirmed", | ||
| failed: "delegation_transaction_failed", | ||
| }, | ||
| unbond: { submitted: "unbonding_or_exit_started" }, | ||
| rebond: REDELEGATION_EVENTS, | ||
| rebondFromUnbonded: REDELEGATION_EVENTS, | ||
| }; | ||
|
|
||
| /** | ||
| * Tracks the funnel events for a submitted contract interaction: submitted | ||
| * right away, then confirmed or failed once it is mined. The receipt is | ||
| * awaited outside React, so the result is still tracked if the sending | ||
| * component unmounts, e.g. when the mobile delegate sheet is closed. | ||
| * "Move Delegated Stake" is a `bond` but counts as a redelegation. | ||
| */ | ||
| export const trackTransaction = ( | ||
| config: Config, | ||
| id: TransactionIdentifier, | ||
| args: InputData, | ||
| hash: `0x${string}` | ||
|
ECWireless marked this conversation as resolved.
|
||
| ) => { | ||
| const isRedelegation = id === "bond" && args.isTransferStake; | ||
| const events = isRedelegation ? REDELEGATION_EVENTS : DELEGATION_EVENTS[id]; | ||
|
|
||
| if (!events) { | ||
| return; | ||
| } | ||
|
|
||
| trackVercelAnalyticsEvent(events.submitted); | ||
|
|
||
| const { confirmed, failed } = events; | ||
| const client = getPublicClient(config); | ||
|
|
||
| if ((!confirmed && !failed) || !client) { | ||
| return; | ||
| } | ||
|
|
||
| // Replacing the transaction in the wallet with a cancel, or with an | ||
| // unrelated transaction, means this interaction never happened, so neither | ||
| // outcome is tracked. Speeding it up is the same interaction and counts. | ||
| let isAbandoned = false; | ||
|
|
||
| client | ||
| .waitForTransactionReceipt({ | ||
| hash, | ||
| timeout: 0, | ||
| onReplaced: ({ reason }) => { | ||
| isAbandoned = reason !== "repriced"; | ||
| }, | ||
| }) | ||
| .then((receipt) => { | ||
| if (isAbandoned) { | ||
| return; | ||
| } | ||
|
|
||
| const event = receipt.status === "success" ? confirmed : failed; | ||
|
|
||
| if (event) { | ||
| trackVercelAnalyticsEvent(event); | ||
| } | ||
| }) | ||
| // A receipt we can't fetch, e.g. because of an RPC error, says nothing | ||
| // about whether the interaction succeeded. | ||
| .catch(() => undefined); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.