<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yaniv Goldenberg</title>
    <description>The latest articles on DEV Community by Yaniv Goldenberg (@yanivgoldenberg2).</description>
    <link>https://dev.to/yanivgoldenberg2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4259470%2F5dfa94f2-41d6-4cd6-8a26-895f2d5e0ec8.png</url>
      <title>DEV Community: Yaniv Goldenberg</title>
      <link>https://dev.to/yanivgoldenberg2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yanivgoldenberg2"/>
    <language>en</language>
    <item>
      <title>Tracking WhatsApp Leads End-to-End: Ref Tokens and PostHog on a WordPress Site</title>
      <dc:creator>Yaniv Goldenberg</dc:creator>
      <pubDate>Mon, 23 Nov 2026 00:45:50 +0000</pubDate>
      <link>https://dev.to/yanivgoldenberg2/tracking-whatsapp-leads-end-to-end-ref-tokens-and-posthog-on-a-wordpress-site-1ke</link>
      <guid>https://dev.to/yanivgoldenberg2/tracking-whatsapp-leads-end-to-end-ref-tokens-and-posthog-on-a-wordpress-site-1ke</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Ref tokens in the CTA
&lt;/h2&gt;

&lt;p&gt;WhatsApp's click-to-chat links (&lt;code&gt;wa.me&lt;/code&gt; / &lt;code&gt;api.whatsapp.com&lt;/code&gt;) 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 &lt;code&gt;crypto.getRandomValues&lt;/code&gt; and cached in &lt;code&gt;sessionStorage&lt;/code&gt;, so CTAs clicked within the same tab session share one ref.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Two caveats worth knowing up front:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two implementation details that mattered:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  2. One event into PostHog, with placement
&lt;/h2&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The part nobody warns you about: filter your own traffic first
&lt;/h2&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this gets you
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;If your buyers talk to you in chat apps, it is worth checking whether your funnel counts them at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;WhatsApp Help: &lt;a href="https://faq.whatsapp.com/5913398998672934" rel="noopener noreferrer"&gt;click-to-chat links and prefilled messages&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PostHog docs: &lt;a href="https://posthog.com/docs/product-analytics/capture-events" rel="noopener noreferrer"&gt;capturing events&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;MDN: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage" rel="noopener noreferrer"&gt;Window.sessionStorage&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Yaniv Goldenberg is a Fractional CMO &amp;amp; Growth Consultant. More on his work: &lt;a href="https://yanivgoldenberg.com/" rel="noopener noreferrer"&gt;yanivgoldenberg.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>analytics</category>
      <category>posthog</category>
      <category>wordpress</category>
    </item>
  </channel>
</rss>
