DEV Community

Yaniv Goldenberg
Yaniv Goldenberg

Posted on Edited on AI-assisted

Tracking WhatsApp Leads End-to-End: Ref Tokens and PostHog on a WordPress Site

My consulting site's main CTA is "message me on WhatsApp". That is a classic analytics hole: the click fires on-site, the conversation happens in WhatsApp, and the lead shows up in reports as "direct / none". Here is an implementation I built and documented for it, plus what the first audit of its event stream showed. Small client-side script, no server-side mess.

1. Ref tokens in the CTA

WhatsApp's click-to-chat links (wa.me / api.whatsapp.com) can carry a prefilled first message. On page load, a script mints a random 4-character ref token per tab session - digits plus capital letters, with the ambiguous ones (I, L, O, U) removed - and appends it in parentheses to the end of that prefilled message. The token is minted with crypto.getRandomValues and cached in sessionStorage, so CTAs clicked within the same tab session share one ref.

When the lead sends the message without editing the prefill, the token arrives inside the first WhatsApp message itself. That is what makes it possible to tie a real conversation back to the session and page it likely came from - no forms, no extra fields.

Two caveats worth knowing up front:

  • The prefill is only a suggestion. A user can edit or delete it before sending, and then no token arrives. The ref is a hint for reconciliation, not a guarantee.
  • Four characters is a small space. Tokens can collide across sessions, so treat a match as strong evidence rather than proof, and reconcile against timestamps too.

Two implementation details that mattered:

  • The script re-sweeps the DOM on DOMContentLoaded and load, because the page builder (Elementor) injects header, footer and floating-button templates late. A data-yg-ref guard keeps the stamping idempotent so the sweep plus the click handler never double-append.
  • The click handler re-stamps the link before navigating, so even a first click on a freshly injected button carries the ref.

2. One event into PostHog, with placement

On click, the same handler fires a single lead_captured event into PostHog with sendBeacon, carrying the ref, the page path, the CTA placement (header, footer, floating button, or which body section), the link URL and the link text.

Important: this event measures CTA clicks, not chats. It turns "someone clicked a green button somewhere" into a per-CTA, per-page count of click intent. Actual conversations still have to be reconciled afterwards, by matching the ref tokens that survive in incoming first messages against the events that minted them.

3. The part nobody warns you about: filter your own traffic first

The first audit of the event stream was humbling. In a two-day window, 47 of 48 lead_captured events were flagged as internal or synthetic rather than buyer clicks. Without an explicit is_test flag (a ?yg_test=1 URL param that persists in localStorage) and filtering internal traffic, most of the "results" would have been noise.

If you instrument something like this, build the test flag on day one. It is much harder to trust a metric you have to clean retroactively.

What this gets you

With this in place, WhatsApp CTA clicks are measurable per placement and per page, and real conversations can be reconciled against sessions through the ref tokens that survive in first messages - clicks measured on-site, messages reconciled via the token.

If your buyers talk to you in chat apps, it is worth checking whether your funnel counts them at all.

References


Yaniv Goldenberg is a Fractional CMO & Growth Consultant. More on his work: yanivgoldenberg.com

Top comments (0)