# Conversion tracking — install guide for each client website

The platform reports three things separately:

| Event | Meaning | Counted as a lead? |
|---|---|---|
| `generate_lead` | An enquiry form was **successfully** submitted (server confirmed it was saved/sent) | **Yes**, a "confirmed form inquiry" |
| `whatsapp_click` | A visitor clicked a WhatsApp link (`wa.me`, `api.whatsapp.com`) | No. It shows intent, not a conversation |
| `phone_click` | A visitor clicked a `tel:` link | No. It shows intent, not a completed call |

Reports always show confirmed form inquiries separately from clicks and split organic search from other channels, with a previous-month comparison. Months before tracking was installed show "not tracked", never 0.

## 1. Prerequisites

- GA4 (gtag.js) **or** Google Tag Manager is already on every page of the site.
- In the platform: the website's **GA4 property ID** (numeric, from GA4 → Admin → Property details) is set, and the Intelliworkz service account has **Viewer** access to that property (Settings → Integrations explains the setup).

## 2. Add the snippet

Website → **Leads & tracking** tab → copy the snippet. Paste it once, just before `</body>`, in the main layout:

- Laravel: `resources/views/layouts/app.blade.php` (or whichever layout every page extends)
- Plain PHP: the shared footer include
- WordPress: theme footer, or a "header & footer scripts" plugin

Pasting it twice is harmless: the second copy does nothing.

WhatsApp and phone clicks are tracked automatically from that moment. The snippet sends **only** the event type, an optional form id, an optional product id and the page. It never reads form fields and never sends names, email addresses, phone numbers or message text to GA4.

## 3. Count form leads only after a successful submission

Choose the pattern that matches the form. Do **not** track the submit button click.

**A. Laravel form that redirects after saving (most common)**

```php
// Controller, after the enquiry is saved and the e-mail queued:
return back()->with('iwz_lead', ['form_id' => 'contact', 'id' => $enquiry->id]);
```
```blade
{{-- Layout, after the snippet: --}}
@if(session('iwz_lead'))
<script>iwzTrack.lead(@json(session('iwz_lead')));</script>
@endif
```
The flash value exists for exactly one request, and `id` is remembered for the browser session, so reloading the page never counts the lead twice.

**B. AJAX / Livewire / fetch form**

```js
fetch('/enquiry', {...}).then(r => r.json()).then(res => {
  if (res.ok) iwzTrack.lead({ form_id: 'contact', submission_id: res.id });
});
```

**C. Redirect to a thank-you page**

```html
<body data-iwz-lead-page="contact">
```
It fires once per visit; reloads and back-button visits are ignored. Make sure the thank-you page is only reachable after a real submission.

## 4. Product pages (optional, for product-wise reports)

Add the product's ID/SKU to product pages:
```html
<meta name="iwz-product-id" content="PX-200">
```
or put `data-iwz-product="PX-200"` on the product container. Product pages are also recognised from Product structured data or from the website's **Product URL patterns** (website settings).

## 5. GA4 configuration (once per property)

1. GA4 → Admin → **Events**: mark `generate_lead` as a **key event**. Do *not* mark `whatsapp_click` / `phone_click` as key events if you want GA4's own conversion reports to show confirmed leads only.
2. GA4 → Admin → **Custom definitions** → create event-scoped dimensions `form_id`, `product_id`, `link_location` (needed to break down events in GA4's own interface; the platform's page-level report works without them).

## Platform test inquiries (inquiry-form monitoring)

When an Admin approves automated tests for a form, the platform occasionally submits it with values labelled `[IWZ-TEST-yymmdd-XXXX]` and an e-mail like `seo-test+iwz-test-…@intelliworkz.tech`. These are posted server-side, so the snippet above never runs and **no `generate_lead` event reaches GA4**. They do reach the client's inbox/CRM: ask the client's team to ignore or filter messages containing `IWZ-TEST`. If the site's own backend sends conversions to GA4 or an ad platform server-side (Measurement Protocol, Conversions API), exclude submissions whose message contains `IWZ-TEST` there.

## 6. Verify

1. Website → Leads & tracking → **Test tracking now**: confirms the snippet and GA4 tag are on the homepage and, if GA4 is connected, reads the GA4 **Realtime** API for the last 30 minutes.
2. Open the site with `?iwz_debug=1`. Every event is logged in the browser console and sent with `debug_mode` so it appears in GA4 → Admin → **DebugView**.
3. Click a WhatsApp link and a phone link, submit a real test enquiry, then press **Test tracking now** again: you should see `whatsapp_click`, `phone_click` and `generate_lead`.
4. The tracking status moves to **Receiving events** after the next daily GA4 sync (05:45).

Status values: *Not installed*, *Snippet found — no events received yet*, *Receiving events*, *No events in 14 days — check the site*, *GA4 not connected — cannot confirm*.

## What was verified before delivery

In a real Chromium browser against a test page: a double click on WhatsApp produced one event; WhatsApp and phone clicks are separate events; clicking Submit alone produced no lead; a duplicate `lead()` call produced one event; reloading a page that replays the success code produced nothing; a thank-you page fired once and ignored reloads; with the snippet pasted twice there was still one event per action; and no email, phone number or message text appeared in the data sent. The GA4 sync was verified against simulated GA4 API responses. **It has not yet been run against a live GA4 property.**
