crabtalk/analytics
Website funnels and channel attribution for Stripe revenue
Readme
Analytics
Analytics reports website traffic, funnels, acquisition, and Stripe revenue. A shared deployment buffers page views until your account next calls it. The service keeps your visits, events, and payments in your account’s caller storage and uses your granted Stripe key to sync revenue.
When the service is running and accessible, call it with Crab or connect your agent through MCP. After verifying your domain and adding the snippet, this call moves buffered visits into your storage and returns visitor counts and conversion rates for the funnel.
crab call crabtalk/analytics funnel '{"steps":["/","/pricing*","signup"],"since":0}'
If the image is available to your account, select Deploy or run crab deploy crabtalk/analytics to start a private copy. You can also deploy from source. A private copy starts with its own storage. Use its running address in calls, app registration, the snippet, and the secret grant.
Set up tracking
-
Register an anonymous app for the origin that serves the snippet. Include the
www.prefix in the origin if the site uses it. Keep the app ID for the snippet.crab app create site-analytics --origin https://example.com --service crabtalk/analytics --anonymous -
Call
claimfor your domain. Publish the returned token as plain text athttps://example.com/.well-known/crabtalk-analyticsand callclaimagain. The bare domain and itswww.version share one claim. The service checks the bare domain first, then thewww.host, and returns the URL that served the token. If the bare domain redirects towww., publish the token on thewww.host. If you previously verified a claim keyed by thewww.host, callclaimagain to verify the bare domain. If the domain already belongs to another account, the refusal carries a fresh token to publish on either host. A transfer clears visits that were still buffered for the former owner. -
Add the snippet to your site with the app ID filled in. The site owner manages consent for browser storage.
-
Use an account API key on your backend to call
eventfor actions such as signups. Pass the visitor and session IDs from the browser. The event inherits the visitor’s latest channel, ordirectif the visitor has no earlier hits.
<script>
(() => {
const app = "<app id>";
const id = (store, key) => store.getItem(key) || (store.setItem(key, crypto.randomUUID()), store.getItem(key));
let visitor, session;
try { visitor = id(localStorage, "crabtalk_vid"); session = id(sessionStorage, "crabtalk_sid"); } catch {}
const q = new URLSearchParams(location.search);
const track = (extra = {}) => fetch("https://api.crabtalk.ai/services/crabtalk/analytics/tools/track", {
method: "POST",
headers: { "Content-Type": "application/json", "X-App": app },
body: JSON.stringify({
domain: location.hostname, visitor, session: session || "none", path: location.pathname,
referrer: document.referrer || null,
utm: { source: q.get("utm_source"), medium: q.get("utm_medium"), campaign: q.get("utm_campaign") },
...extra,
}),
keepalive: true,
});
track();
window.crabtalk = { track: (event, value, currency) => track({ event, value, currency }) };
})();
</script>
Single-page apps call crabtalk.track() on navigation. If the browser has no stored visitor ID, the service tries the host’s daily client key. CrabTalk Cloud supplies that key when it receives the app call’s client address. If the host supplies no key, visits without a visitor ID are refused.
Connect Stripe revenue
-
Copy the browser IDs into Stripe metadata as
crabtalk_visitor_idandcrabtalk_session_id. For payment-mode Checkout, set both the session’smetadataandpayment_intent_data.metadata. For subscription-mode Checkout, set both the session’smetadataandsubscription_data.metadata. -
Create a Stripe restricted key with read access to Checkout Sessions, PaymentIntents, Customers, and Invoices. Request a runtime secret grant and approve it on the returned link.
crab secret resource request crabtalk/analytics "Stripe analytics" --host api.stripe.com -
Call
connect_stripewith the approvedsecret_id. Usesync_revenueto import revenue orchannelsto sync and report together.
Stripe sync reads newly created objects in bounded windows and saves progress after each completed window. For an initial import, call sync_revenue again while has_more is true. synced_through gives the oldest completed endpoint cursor in Unix seconds. If Stripe fails, sync_revenue includes the error and keeps the completed progress. channels still reports from storage and shows revenue_synced_through and revenue_fully_synced when Stripe is connected.
A window with more than two Stripe pages is split. A single second with over 200 objects at one endpoint cannot be split further and leaves that endpoint's cursor unchanged. An object that completes or is paid after its creation timestamp passes the cursor can be missed. Revenue records gross payments and subscription renewals, excluding later refunds.
Reports
funnel, channels, and stats take an inclusive since and an exclusive until in Unix seconds. Omit until to use the current time.
funnelcounts visitors who completed two to eight page or event steps in order within the window.channelsreports visitors, paying customers, and revenue by currency. Setbytochannel(the default),source,medium,campaign, orlanding. Each row'schannelfield holds the grouping value. First touch uses the earliest hit. Last touch uses the latest hit before the visitor's first payment in the window, or their latest hit if unpaid. For either model,landinguses the first page view in the window among eligible hits. Missing values and unmatched payments appear asunattributed.statscompares totals with the immediately preceding period of equal length and returns populated UTC calendar buckets for the current window. Setintervaltoday(the default) orweek(starting Monday). Visitors and payers are counted distinctly in each period. Conversion is paying customers divided by visitors, including payments without a visit. Revenue and revenue per visitor use minor units by currency. Bounce rate is the share of sessions that start in the period and have exactly one recorded page view, including views outside the period when checking that count. Rates with no denominator are omitted.journeyaccepts a Stripe customer ID or a visitor ID incustomer. It shows each visit's time, landing page, channel, and pages, along with payment records in chronological order. It reports elapsed days from the first visit to the first payment when both exist and the payment follows the visit. The report keeps the earliest 25 visits, 25 pages per visit, and 100 payments, with a flag for each list that was cut. Long lists of visits or payments can reach the response size limit sooner.
Revenue reports use stored Stripe payments. Call sync_revenue before stats or journey to refresh them. channels runs a bounded sync when Stripe is connected.
Storage and limits
The shared buffer keeps up to 10,000 visits per verified domain. Each owner call moves up to 5,000 rows. A retry after a partial commit cannot duplicate hits. Download and delete in Storage cover rows that have reached your caller file.
Browser events can be forged. Keep conversion events you need to trust on your backend. Analytics tools are free to call. App calls and storage count toward the applicable account limits.