Help center › Data and accounts

Website code and events

Put the Megametrics code on your website and send sign-ups and payments from the website, your server, Stripe, Shopify and other services — to see which ad clicks led to them.

Connect a website

On the Sources page, in the Website and events panel, click Connect website:

  1. Domains — one per line, without https:// or a path: example.com. Subdomains are covered: with example.com, hits from shop.example.com are accepted too.
  2. Checkout domains — if payment happens on another domain, such as pay.example.net. The field is optional. The code adds the visitor ID to links to these domains, so the visit and the payment are linked. The code must be on the checkout pages too.
  3. Consent — Consent first (the default) or Without asking for consent, see below.
  4. Click Save — the code to paste appears.

Install the code

<script>window.mm=window.mm||function(){(mm.q=mm.q||[]).push(arguments)}</script>
<script async src="https://megametrics.app/t.js" data-site="SITE_KEY" data-consent="required"></script>

Copy the code in the panel with Copy: it already holds your site key and the consent mode you chose. The code must be on every page, best in <head>. The first line lets you call mm(...) before the script has loaded: the calls wait for it. One page holds one Megametrics code: of two codes with different site keys, only one works.

  • HTML — paste the code before </head> on every page or in the shared template.
  • Google Tag Manager — a new Custom HTML tag with this code and the All Pages trigger, then publish the container.
  • WordPress — a plugin that adds code to <head>, or the theme's header.php, before </head>.
  • Shopify — Online Store → Themes → … → Edit code, the theme.liquid file, before </head>. Checkout pages don't run theme code: orders come from the Shopify webhook (below).
  • Tilda — Site Settings → More → HTML code for the HEAD section, then publish all pages again.
  • Webflow — Site settings → Custom code → Head code, then publish the site.

Check installation — Megametrics opens the home page of the first domain and looks for the code with your site key; the answer appears under the button. The check can't find code added through Google Tag Manager: it appears on the page only in the browser. Then watch the status line — after the first visit it changes to “The code works”.

Consent first (the default): until the page calls mm('consent', true), the code writes nothing to the browser — no cookies, no storage — and sends nothing. Calls made before consent wait in the page's memory and go out after it. mm('consent', false) drops them and, if consent was given before, also deletes the code's cookies. Consent is remembered in the _mm_c cookie for 395 days, so there is no need to repeat the call on every page.

Call it from your cookie banner when the visitor answers:

mm('consent', true)   // agreed
mm('consent', false)  // declined

There are no ready-made links to consent services yet — make this call from your banner's handler.

Without asking for consent — the code works at once. Choose it only if the law of your visitors' country allows it. In the EU such an identifier usually needs consent.

Events

On its own, the code sends only page views: on load and when the address changes without a reload (history.pushState, replaceState, the Back button). Add sign-ups and payments with a call on the website:

mm('event', 'Lead')
mm('event', 'Purchase', { value: 120.5, currency: 'USD', order_id: 'A-1' })
  • An event name is up to 80 characters. Lead, Sign up, Start trial, Purchase, Schedule, Subscribe and Complete registration count as conversions from the start. Mark other events with the Conversion switch in the events table.
  • value is the value, currency the three-letter ISO 4217 code, order_id the order number.
  • An event with the same name and the same order_id counts once, wherever the repeat comes from: the browser, your server or a webhook. Reloading the “Thank you for your order” page won't double the payment.

Who the visitor is: identify

After a form or a sign-in, pass the email or the phone:

mm('identify', { email: 'anna@example.com', phone: '+4915112345678' })

The code turns them into SHA-256 hashes right in the browser: the email and phone themselves never reach the server. The hash links one person's visits from different devices and browsers: if the same email comes from another visitor, their histories merge into one. Write the phone in international format, with the country code: only digits and the leading “+” go into the hash, so 0151 … and +49 151 … give different hashes.

A token for server events

A payment is often confirmed on the server, not on the page. To link it to the visit and its ad click, take the visitor's token on the website and pass it to your server — for example, in a hidden form field:

mm('token', function (token) {
  document.querySelector('input[name="mm_token"]').value = token
})

Your server sends the token in the event's mm_token field. For Stripe Checkout, put it in the session's metadata.mm_token (or in client_reference_id, if that field is free). There is no token before the visitor consents.

Events from your server

POST /api/projects/<project>/events with an API key that has Can also send events ticked. The owner or an admin creates keys on the AI and API page, see Claude, ChatGPT and the API. Like any API key, such a key also reads all the workspace's data: give it to a forms service or a CRM only if you trust them.

curl -X POST -H "Authorization: Bearer $MEGAMETRICS_KEY" -H "Content-Type: application/json" \
  -d '{"event_name": "Purchase", "mm_token": "<token from the website>", "email": "anna@example.com",
       "value": 120.5, "currency": "USD", "order_id": "A-1"}' \
  https://megametrics.app/api/projects/<project>/events
  • event_name — required, 1 to 80 characters.
  • event_time — an ISO date or Unix seconds; the time of receipt by default.
  • mm_token — the visitor's token from the website.
  • email or email_sha256, phone or phone_sha256 — the email and phone as is or as a SHA-256 hash. Before hashing, lower-case the email and trim the spaces at its ends; keep only the digits and the leading “+” of the phone.
  • external_id — your own ID of the person, such as the customer number in your CRM.
  • value and currency — the value and a three-capital-letter currency code, such as USD.
  • order_id — the order number.
  • idempotency_key — a key for retries.

An email or phone sent as is is hashed on receipt and never stored — not in the database, not in the logs.

Repeats: with order_id, an event counts once per name and order; without it, once per idempotency_key; without both, every call is a new event. The answer is {"id": "…", "duplicate": false}; true means the event was already there and the second one wasn't recorded.

Webhooks: Stripe, Shopify and any service

In the Website and events panel, under Webhooks, create a receiver — the panel shows the address to paste. A receiver's card shows when it last received something and the last error. A request with a wrong signature is rejected. If a correctly signed request can't be read, the error appears in the card, and the sender still gets an “accepted” answer so that it doesn't retry forever.

Stripe and Shopify show the signing secret only after the address has been added there. So their card gives the address first, and the secret is pasted after. Until the secret is saved, requests to the address aren't accepted.

Stripe

  1. Click Connect Stripe and copy the address from the card.
  2. In Stripe, open Webhooks and add a new endpoint: paste the address and choose the events checkout.session.completed, checkout.session.async_payment_succeeded, customer.subscription.created and invoice.paid.
  3. Copy the endpoint's Signing secret (it starts with whsec_), paste it into the card and click Save secret.

What Stripe events become:

  • A paid checkout.session.completed → Purchase: the amount, the currency, the buyer's email, the session ID as order_id; the visitor's token from metadata.mm_token or client_reference_id. Amounts in currencies without minor units, such as JPY and KRW, aren't divided by 100. A session without a payment — a trial, a card saved for later — isn't a purchase.
  • With delayed payment methods (SEPA Direct Debit, ACH, Boleto), the session completes unpaid, and Purchase comes with checkout.session.async_payment_succeeded, when the money arrives.
  • customer.subscription.created with the trialing status → Start trial.
  • invoice.paid → Subscribe, once per subscription: for the first paid invoice of a new subscription; with a trial — for the first payment after it.

Other Stripe events are accepted and skipped.

Shopify

  1. Click Connect Shopify and copy the address from the card.
  2. In Shopify: Settings → Notifications → Webhooks → Create webhook, the Order creation event, JSON format, the address from the card.
  3. Below the list of webhooks, Shopify shows the key they are signed with. Copy it, paste it into the card and click Save secret.

A new order becomes a Purchase event: the order total, the currency, the buyer's email, the order number. If the address of the visit's first page (landing_site) had utm_* labels or an ad click ID, the buyer gets a touch at the time of the order. So the last click shows even without code in the shop's theme.

Any service

  1. Create the Any service card: the event name (such as Lead) and the paths to fields in the request body, with dots: data.customer.email. Paths can be set for email, phone, value, currency, order_id, mm_token, event_time, external_id and idempotency_key.
  2. The panel shows the address and a secret. The secret is shown once — save it right away.
  3. The service signs every request: the header X-Megametrics-Signature: sha256=<hex>, where <hex> is the HMAC-SHA256 of the request body with the secret.

Services repeat a delivery after a timeout. So without order_id and idempotency_key, a request with exactly the same body counts once.

body='{"data": {"customer": {"email": "anna@example.com"}, "total": 120.5}}'
sig=$(printf '%s' "$body" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')
curl -X POST -H "Content-Type: application/json" -H "X-Megametrics-Signature: sha256=$sig" \
  -d "$body" https://megametrics.app/hooks/<id>

What you see and where

Everything is in the Website and events panel on the Sources page:

  • The status line: “The code hasn't been seen on the website yet”, “The code works” with the visits of the last 24 hours, “Not a single hit in a day”, or “Hits come from a domain not on the list” — with an Add domain button. When the website needs attention, a Website mark appears in the summary at the top of the page.
  • Events — the name, where it comes from (website, server, Stripe, Shopify, webhook), how many in 7 days, the last one and the Conversion switch.
  • Log — the last 50 hits and events; while the log is open, it refreshes every 5 seconds. New visits show their source and channel. Hashes show only their first 6 characters.
  • Visits this month — how much of the plan's limit is used. The same counter is on the Plan page.

The Overview has a Website: where people come from and who pays panel: visitors, visits, conversions, visit conversion rate, value and value per visitor for the period with the change against the previous one, a chart by day, and Channel, Source, Campaign, Entry page and Device tabs. Conversions and value follow the “Last non-direct click”, 30 days model. A tab row opens Explorer with that filter. When nothing comes from the site, the panel says why: the code is waiting for consent, hasn't sent a visit yet, hits come from a domain not on the list, or a day of silence. The default period ends yesterday, so today's visits are shown on a separate line.

Explorer has a Website: own code dataset: the same visitors, visits, conversions and value broken down by channel, source, utm_*, ad platform, referrer, entry page, hostname, device and event, with the model, window and date basis of the Attribution dataset. It works in boards, exports, the API and MCP ("dataset": "site").

Which campaigns led to the sign-ups and payments is on the Campaigns page, in the Attribution view, with each buyer's journey — see Attribution and customer journeys. The same conversions can be sent back to Meta and Google Ads — see Sending conversions to ad accounts.

A visit is a new session. It starts when the visitor has been inactive for 30 minutes or arrives by a link with utm_* labels or an ad click ID. Each visit records a channel:

  • paid — an ad click ID or a paid utm_medium: cpc, ppc, paid, cpm, display, retargeting and the like;
  • email — utm_medium=email;
  • search — from Google, Bing, Yandex, DuckDuckGo and other search engines;
  • social — from Facebook, Instagram, X, LinkedIn, VK, TikTok, YouTube, Telegram and other social networks;
  • referral — from any other website (a move from your own domains and checkout domains isn't a referral);
  • direct — with no source.

Ad click IDs: gclid, gbraid, wbraid — Google Ads; fbclid — Meta; ttclid — TikTok; msclkid — Microsoft Advertising; li_fat_id — LinkedIn; yclid — Yandex Direct; ScCid — Snapchat; twclid — X; epik — Pinterest. fbclid alone doesn't make a visit paid: Facebook adds it to ordinary links too. A paid utm_medium or a utm_id is needed as well. How to put labels in your ads — see Labels in ad links.

A viewer sees the whole panel. A member also connects the website, marks conversions and creates webhooks. Admins and the owner also stop collecting and delete a person's data.

Limits

  • Blockers. Ad and tracker blockers can stop the code, and some visits won't arrive. Blockers don't see events from your server or webhooks: sending sign-ups and payments that way is more reliable.
  • Safari keeps cookies set by a script for no more than 7 days. Someone who comes back after a week looks like a new visitor: their earlier click links to the purchase only if they leave the same email or phone again.
  • No browser fingerprint. We recognize a person only by our cookie, ad click IDs and the hash of the email or phone. So we recognize fewer people than services that fingerprint — on purpose.
  • Visits limit per month per workspace: trial — 100,000, Starter — 10,000, Growth — 100,000, Agency — 500,000. The month is the calendar month in UTC. Above 120% of the limit, page views aren't accepted until the month ends; events are always accepted. The plan never upgrades itself.
  • Rate — up to 30 hits a second per website and up to 10 a second from one address; the rest are rejected.
  • Bots. Hits from search robots, link preview services and browsers driven by programs aren't accepted.
  • Other domains. Hits from domains not on the list aren't recorded; their count shows in the status line.

What we keep and for how long

  • Hits — the page address, where the visitor came from, the device type — 30 days. The values of address parameters with an email, a phone or a name (email, phone, name, first_name and the like, or a “+” with digits) are cleared: a form sent by GET leaves no contacts in the address.
  • Visits with their channel, labels, ad click ID and landing page, events with their value, and the links between visitors, people and the hashes of their email and phone — 25 months.
  • Not kept: the IP address, the country, a browser fingerprint, the email and phone as is. The device type — phone, tablet or computer — is taken from the User-Agent on receipt. The User-Agent itself is kept only with events from the website, not with page views, for as long as the event: Meta needs it when conversions are sent to it.

Cookies the code sets on your website, under your domain:

  • _mm_v — the visitor ID, 395 days;
  • _mm_s — the session ID, lives 30 minutes without activity;
  • _mm_c — the visitor agreed, 395 days.

To link domains, the code adds the _mm parameter with the visitor ID and the time of the click to links to checkout domains. The checkout page accepts it for 2 minutes and removes it from the address: a link forwarded to someone later doesn't carry the visitor over.

You are the controller of your visitors' data, and Megametrics is the processor acting on your behalf. The terms are in the Data Processing Agreement; what we tell visitors is in the Privacy Policy. Describe the code and its cookies in your own policy, and ask for consent where the law requires it.

Stop collecting (admins and the owner) removes the website's settings: hits are no longer accepted, and what was collected stays until the end of its retention period. Sending conversions to Meta and Google Ads turns off too. Deleting the project deletes everything.

Delete a person's data

An admin deletes everything about a person — hits, visits, events and links — in the Delete a person's data form at the bottom of the panel: by email, phone or the visitor ID from the _mm_v cookie. The form hashes the email and phone right in the browser.

Through the API — with the admin's session:

DELETE /api/projects/<project>/people?email=<SHA-256 of the email>

Instead of email, pass phone (also a SHA-256 hash), external_id (your own ID as is) or visitor. The email and phone go only as a hash, by the same rules as for server events: the request address is written to server logs, and the email itself must not get there. The answer says how many rows were deleted in each table. Backups lose the data within 30 days. Copies of touches and events already transferred to a warehouse aren't deleted: delete them in the warehouse.

Visitors from Russia

Russian Federal Law 152-FZ requires personal data of Russian citizens to be first recorded in databases on servers in Russia. The Megametrics server is in Germany. So for now, don't put the code on websites for visitors from Russia, and don't send events about them from your server or webhooks.

Next: Sending conversions to ad accounts

Didn't find the answer?

Write to us — a person replies within one working day.