# Supabase and Lovable

Put the tracking tag in a Lovable or Vite app, track sign-ups and purchases in the browser, and confirm them from a Supabase Edge Function.

A Lovable app is a Vite single-page app on Supabase. The tag goes in its `index.html`, and your Supabase backend confirms each purchase, so a purchase still counts when the browser blocks the tag.

## Before you start

- A tracking site for the app's domain, and its snippet from the site's install options.
- A Zernio API key and a webhook secret of your choice, kept as Supabase secrets: `supabase secrets set ZERNIO_API_KEY=... ZERNIO_WEBHOOK_SECRET=...`.

## Install the tag

Give Lovable this prompt, with your site's snippet in place of the placeholder:

```text
Add this script tag inside <head> in index.html, exactly as written:
<SNIPPET>
After a user signs up, call window.zernio?.("track", "CompleteRegistration", { eventId: "signup_" + user.id }).
After a payment succeeds, call window.zernio?.("track", "Purchase", { eventId: "order_" + order.id, value: order.total, currency: order.currency }).
```

Publish the app, then open it once: the site turns Live with that visit. On a plain Vite app, paste the snippet in `index.html` yourself.

## Confirm purchases from Supabase

Create an Edge Function that sends each new order to [`POST /v1/events`](/conversions/send-events) with the same `eventId` the browser used, so the two count once.

Put your site's profile id in `profileId`, and send `value` in currency units, not cents.

Both sides must build `eventId` from the same id: if a Stripe webhook creates the order, store the checkout session id on it and use that in the browser too.

```typescript
// supabase/functions/zernio-order/index.ts
Deno.serve(async (req) => {
  if (req.headers.get('x-webhook-secret') !== Deno.env.get('ZERNIO_WEBHOOK_SECRET')) {
    return new Response('Unauthorized', { status: 401 });
  }
  const { type, record } = await req.json();
  if (type !== 'INSERT') return new Response('Ignored');
  const response = await fetch('https://zernio.com/api/v1/events', {
    method: 'POST',
    headers: { Authorization: `Bearer ${Deno.env.get('ZERNIO_API_KEY')}`, 'Content-Type': 'application/json' },
    body: JSON.stringify({
      profileId: '66a1f0c2a4b9d3e8f1a2b3c4',
      events: [{
        eventName: 'Purchase',
        eventTime: Math.floor(new Date(record.created_at).getTime() / 1000),
        eventId: `order_${record.id}`,
        value: record.total,
        currency: record.currency,
        user: { email: record.email, externalId: record.user_id },
      }],
    }),
  });
  return new Response(await response.text(), { status: response.status });
});
```

Deploy it with `supabase functions deploy zernio-order`.

In Supabase, open Database, Webhooks, and create a webhook on the `orders` table for Insert events that calls the `zernio-order` Edge Function, with an `x-webhook-secret` header set to your webhook secret.

The secret keeps anyone holding your app's public key from sending fake purchases.

## How it behaves

- The browser event and the server event share `eventId`, so each order counts once on every platform.
- The server event carries the email and user id, which Zernio hashes before any platform sees them. Read the email from the order or the user's profile if `orders` has no email column.
- To join the server event to the ad click, also send the visitor id as described in [join backend events to the visit](/products/server-side-tracking/backend-events).

## Related

- [Install the tracking tag](/products/server-side-tracking/install)
- [Deduplication](/products/server-side-tracking/deduplication)
- [Send events](/conversions/send-events)