← Blog

·

WhatsApp button on your website with GA4 tracking

Updated on November 4, 2026

To add a WhatsApp button to your website and track it in GA4, use a https://wa.me/ link with your number and a prefilled message, style it as a floating button with CSS and fire the gtag generate_lead event on click. GA4 then shows how many WhatsApp clicks each page, channel and campaign produced. To tie the click to the conversation that reaches your system, add a short reference code to the message.

Below you’ll find the full code (HTML, CSS and JavaScript), the GA4 setup and how to match the code with the official API webhook.

How the wa.me link works

The WhatsApp click-to-chat link follows the format described in the WhatsApp Help Center:

  • https://wa.me/5584999999999 opens a chat with the number.
  • https://wa.me/5584999999999?text=Hi%2C%20I%20came%20from%20your%20website opens the chat with the text already typed.

Number rules: full international format (country code + area code + number), digits only, no +, no brackets, dashes or leading zeros. The examples use a Brazilian number (country code 55); replace it with your own. The message must be URL-encoded (a space becomes %20, accented characters become %C3%... sequences). In JavaScript, encodeURIComponent() does it.

If you don’t want to build it by hand, Vectalk’s WhatsApp link generator creates the link with a message and a QR code right in your browser.

Two important points: the link doesn’t send anything by itself (the visitor taps “send”) and the text can be edited or deleted before sending. That’s why tracking has two halves: the click, measured by GA4, and the received message, measured by your system.

Floating button in HTML and CSS

A button fixed to the bottom-right corner, no library needed:

<a id="btn-whatsapp"
   class="btn-whatsapp"
   href="https://wa.me/5584999999999"
   target="_blank"
   rel="noopener"
   aria-label="Chat on WhatsApp">
  Chat on WhatsApp
</a>
.btn-whatsapp {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 9999;
  padding: 14px 20px;
  border-radius: 999px;
  background: #25d366;
  color: #fff;
  font: 600 16px/1 system-ui, sans-serif;
  text-decoration: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.btn-whatsapp:hover { filter: brightness(0.95); }
@media (max-width: 480px) {
  .btn-whatsapp { right: 12px; bottom: 12px; padding: 12px 16px; font-size: 15px; }
}

Details that prevent problems:

  • target="_blank" opens WhatsApp in another tab and keeps your page open, which gives the GA4 event time to go out.
  • Visible text (“Chat on WhatsApp”) makes it clear what the button does and helps screen readers. If you use only an icon, keep the aria-label.
  • Don’t cover other elements. Check on mobile that the button doesn’t hide the buy button, the cookie banner or another tool’s chat widget.

Want to try it while you read? Create a Vectalk account and send your first message through the official API. Create free account →

Prefilled message with page context

The prefilled message helps your agent know what the person wants. Build the text from the page and the visit’s UTM parameters:

const NUMBER = '5584999999999';

function utm(name) {
  const value = new URLSearchParams(location.search).get(name);
  if (value) sessionStorage.setItem(name, value);
  return sessionStorage.getItem(name) || '';
}

function refCode() {
  // Short code, no personal data. E.g. S-7K2QX
  let ref = sessionStorage.getItem('wa_ref');
  if (!ref) {
    ref = 'S-' + Math.random().toString(36).slice(2, 7).toUpperCase();
    sessionStorage.setItem('wa_ref', ref);
  }
  return ref;
}

function whatsappLink() {
  const topic = document.title.split('|')[0].trim();
  const text = `Hi! I came from your website and I'd like to know about: ${topic}. (ref ${refCode()})`;
  return `https://wa.me/${NUMBER}?text=${encodeURIComponent(text)}`;
}

Why a code instead of the full UTM in the message: utm_source=google&utm_medium=cpc&utm_campaign=... makes the text long and odd for the customer, who may delete it. A code like ref S-7K2QX is short, discreet and points to a record on your server with all the visit details.

Never put the visitor’s name, email or any personal data in the message.

The generate_lead event in GA4

generate_lead is an event from the GA4 event reference for when a lead is generated. It accepts currency and value (if you send value, currency is required) and lead_source. With gtag already installed on the site:

const button = document.getElementById('btn-whatsapp');

button.addEventListener('click', () => {
  button.href = whatsappLink();
  const ref = refCode();

  gtag('event', 'generate_lead', {
    lead_source: 'whatsapp_button',
    lead_ref: ref,
  });

  // Optional: store the source on your server to match with the webhook
  navigator.sendBeacon('/api/wa-ref', JSON.stringify({
    ref,
    page: location.pathname,
    utm_source: utm('utm_source'),
    utm_medium: utm('utm_medium'),
    utm_campaign: utm('utm_campaign'),
    created_at: new Date().toISOString(),
  }));
});

Call utm() once on page load too, to store the landing parameters before the visitor moves to another page of the site.

If you use Google Tag Manager, the logic is the same: a click trigger on links containing wa.me and a GA4 event tag named generate_lead.

GA4 setup

  1. Mark it as a key event. In GA4 Admin, on the key events page, mark generate_lead. Google calls actions that matter to your business key events; “conversion” is now the term used for Google Ads campaigns (about key events).
  2. Register custom parameters. To analyze lead_source and lead_ref in reports, create event-scoped custom dimensions. Data shows up after 24 to 48 hours (custom dimensions and metrics).
  3. Value, if it makes sense. If you know what a WhatsApp lead is worth on average, send value and currency. If you don’t, don’t make it up: leave it without a value.
  4. Test in DebugView before publishing and check that the event fires once per click.

With that, GA4 answers which pages, sources and campaigns drive the most WhatsApp clicks.

Closing the loop: from click to conversation

GA4 measures the click. The conversation reaches your system through the official WhatsApp API webhook. The reference code connects the two ends:

// Webhook: look for the code in the contact's first message
export async function handleMessage(msg, db) {
  if (msg.type !== 'text') return;
  const m = msg.text.body.match(/ref (S-[A-Z0-9]{5})/);
  if (!m) return;

  const source = await db.findRef(m[1]); // stored by /api/wa-ref
  if (source) {
    await db.updateContact(msg.from, {
      site_source: source.page,
      utm_source: source.utm_source,
      utm_campaign: source.utm_campaign,
    });
  }
}

From there, your CRM knows that this contact came, for example, from the pricing page through the search campaign. If a sale happens, you credit the right channel. The article on WhatsApp API CRM integration shows where to store these fields.

A match rate below 100% is normal: some people delete the text, others message the number directly without using the button.

What about ads that open WhatsApp?

If traffic comes from Meta click-to-WhatsApp ads, the webhook already includes a referral object with ad data, with no need for a code in the message. See how to track click-to-WhatsApp ads.

Reply fast after the click

The button works with the WhatsApp Business app, with coexistence or with the API only. What changes is what happens after the click. With the API, the customer’s first message opens the 24-hour customer service window and reaches your system via webhook. You can reply automatically, route to the right agent and set up a welcome and away message.

Since October 1, 2026, each number has an allowance of 1,000 service messages per month; from the 1,001st on, a reply inside the window costs BRL 0.0350 for recipients in Brazil.

How to do it with Vectalk

The button and the GA4 event run on your site and don’t depend on your provider. Vectalk’s part is receiving the conversation: the number runs on the official API (with coexistence if you want to keep using the app), webhooks arrive in Meta’s format and your system reads the reference code. Vectalk also exposes Meta’s prefilled-message QR codes, which generate a short wa.me/message/... link per campaign, described in the documentation.

Vectalk has no customer service inbox in its dashboard: conversations go to your system, CRM or the support tool connected to the API. To start, see getting started with Vectalk or create your account.

Frequently asked questions

Can GA4 tell whether the person actually sent the message?

No. GA4 sees the button click. Sending happens inside WhatsApp, outside your site. To know who sent it, match the reference code with the message received via webhook.

Should I use wa.me or api.whatsapp.com?

The WhatsApp Help Center documents the wa.me format. Use it, with digits only in the number and a URL-encoded message.

Can I use generate_lead for other forms too?

Yes. To tell them apart in reports, use different lead_source values (whatsapp_button, contact_form). GA4 also has qualify_lead and close_convert_lead for later funnel stages.

Does the button slow down my site?

The example above loads no library or external image: it’s a link with CSS. Button plugins may load extra scripts and icons; check the impact before installing one.

Does the reference code work with coexistence?

Yes, as long as incoming messages reach your system through the API webhook. See what syncs in WhatsApp coexistence.


Official sources checked on November 4, 2026: WhatsApp: how to use click to chat, GA4 event reference (generate_lead), GA4: about key events, GA4: custom dimensions and metrics, WhatsApp API pricing, Vectalk: Create QR Code, Vectalk: Webhooks.

Put your business on the official WhatsApp

Create your account, connect your number to the official API (you can keep the app with coexistence) and start serving customers and sending messages.