Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Measurement Protocol API secret exposed in frontend: how to fix it

TRK-351High impactCould addBy FunnelGuardPublished Updated
On this page

What this issue is

The Measurement Protocol is a way to send events to Google Analytics 4 (GA4) from a server or app instead of the browser. Each request needs your Measurement ID (or Firebase app ID) and an API secret. You create the secret in GA4 under Admin > Data streams > your stream > Measurement Protocol API secrets.

Example of the mistake: secret in browser codejavascript
fetch('https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXX&api_secret=YOUR_SECRET', {
  method: 'POST',
  body: JSON.stringify({
    client_id: 'abc.123',
    events: [{ name: 'purchase', params: { value: 49, currency: 'USD' } }]
  })
});

Why it costs you leads or sales

Anyone who finds the secret can send events that look real. They can add fake purchases, fake leads or thousands of junk events. Measurement Protocol hits are not tied to a real browser session, so they are hard to tell apart from genuine server events.

How an exposed secret pollutes your data
  1. Secret in page codeapi_secret is readable in source or JS file
  2. Copied by a third partyBot, competitor or curious visitor
  3. Fake events sentFake purchases and leads reach GA4
  4. Ads and reports skewedImported conversions and bidding use bad data

The break is the moment the secret ships to the browser. Everything after it is built on data you cannot trust.

Reported conversions vs real conversionsExample numbers
Reported conversions vs real conversions (example)Real leads: 120 conversions; Fake events: 300 conversions; Reported in GA4: 420 conversions0161322120300420Real leadsFake eventsReported in GA4conversions

Example: a month where fake Measurement Protocol events inflate reported conversions. Numbers are illustrative only.

How to check if your site has it

Look for the secret in your own code
  1. Search the page source

    Open your key pages, press Ctrl+U (Cmd+Option+U on Mac) and search for api_secret and mp/collect.

  2. Search loaded scripts

    In Chrome DevTools open Network, reload, filter by JS, then use Ctrl+Shift+F (Cmd+Option+F) to search all loaded files for api_secret.

  3. Check Google Tag ManagerGoogle Tag Manager

    In your container, use the search box for api_secret and mp/collect. Look at Custom HTML tags and variables.

  4. Check theme and plugin settings

    Search theme files and any analytics or tracking plugin settings for the same strings. Some plugins ask you to paste the secret into a field that is then printed on the page.

  5. Review the secrets in GA4

    In GA4 go to Admin > Data streams > your stream > Measurement Protocol API secrets. Check which secrets exist and who created them.

How to fix it

Rotate the secret and move calls to the server
  1. Create a new secret

    In GA4 go to Admin > Data streams > your stream > Measurement Protocol API secrets > Create. Give it a clear nickname, such as the name of the server that will use it.

  2. Store it on the server

    Put the new secret in an environment variable or your platform's secrets store. Never commit it to a theme, a public repository or a tag.

  3. Move the call to server code

    Send the Measurement Protocol request from your backend, for example after a verified payment or a saved form submission. The browser only talks to your own endpoint.

  4. Remove the secret from the frontend

    Delete it from templates, scripts, tag manager code and plugin fields. Publish the changes and clear caches and CDN caches so old files with the secret are gone.

  5. Delete the exposed secret

    Back in GA4, delete the old secret once the new server-side setup works. This stops anyone using the leaked one.

Server-side example (Node.js 18+): secret stays in an environment variablejavascript
// Runs on your server, never in the browser
async function sendGa4Event(clientId, name, params) {
  const url = `https://www.google-analytics.com/mp/collect?measurement_id=${process.env.GA4_MEASUREMENT_ID}&api_secret=${process.env.GA4_API_SECRET}`;
  await fetch(url, {
    method: 'POST',
    body: JSON.stringify({
      client_id: clientId,
      events: [{ name, params }]
    })
  });
}
Where the secret usually hides, by platform
  1. Check plugins and theme

    Search wp-content/themes and wp-content/plugins for api_secret. Review analytics plugin settings and any code snippet plugin.

  2. Move the call to PHP

    Call the endpoint with wp_remote_post() in a server hook, and define the secret in wp-config.php rather than printing it in a template.

Secret handling before and after

Exposed

  • api_secret visible in page source
  • Browser sends Measurement Protocol calls directly
  • Anyone can send fake purchases
  • Same secret has been live for months

Protected

  • api_secret only in server environment
  • Browser calls your own endpoint
  • Server validates the order or lead before sending
  • Old secret deleted, new one named and tracked

How to confirm it's fixed

Confirmation checks
  • Searching page source and loaded JS for api_secret and mp/collect finds nothing
  • Tag Manager search for api_secret returns nothing in published versions
  • The old secret is deleted in GA4 Measurement Protocol API secrets
  • A test event from your server shows up in GA4 DebugView or Realtime
  • Caches and CDN are purged so old files are not served
  • Event counts for conversions look normal over the next few days

To test the server call, you can send a request to the debug endpoint https://www.google-analytics.com/debug/mp/collect. It returns validation messages and does not record the event.

Validate a Measurement Protocol payload (run from your server or terminal)bash
curl -X POST "https://www.google-analytics.com/debug/mp/collect?measurement_id=$GA4_MEASUREMENT_ID&api_secret=$GA4_API_SECRET" \
  -H 'Content-Type: application/json' \
  -d '{"client_id":"test.123","events":[{"name":"generate_lead"}]}'

How to prevent it happening again

Prevention habits
  • Keep all secrets in environment variables or a secrets manager
  • Add a code review rule: no api_secret in templates, tags or client scripts
  • Add a secret scanner to your repository or build pipeline
  • Name each GA4 secret after its owner and review the list every quarter
  • Rotate the secret when a developer, agency or plugin with access leaves
  • Watch GA4 for event spikes from unexpected sources and unusual conversion counts

How FunnelGuard handles this

FunnelGuard does not check for this yet. Scanning page source for an exposed Measurement Protocol secret is a possible future check, not a current feature.

What FunnelGuard does today is run your real customer journeys on a schedule and check the tracking hits sent to GA4, Google Ads, Meta and other platforms, plus whether test leads reach your inbox and CRM. That helps you notice when expected tracking stops working, but it is not a secret scan.

How a leaked secret typically plays out
  1. Day 0

    Developer adds a client-side Measurement Protocol call with the secret inline.

  2. Week 2

    A bot or visitor copies the secret from the source.

  3. Week 3

    Fake events start to appear; conversion numbers rise without more revenue.

  4. Week 6

    Team notices the gap, rotates the secret and moves calls to the server.

Questions people ask

Is the GA4 Measurement Protocol API secret safe to expose in JavaScript?

No. Google intends the API secret to stay private, because with it and your Measurement ID anyone can send events to your property. Keep it on your server and never place it in page source, a theme or a tag manager tag.

How do I find out if my api_secret is visible on my website?

Open a page, view the source and search for api_secret and mp/collect. Then search loaded JavaScript files in DevTools and your Google Tag Manager container for the same terms. Also check theme files and analytics plugin settings.

How do I rotate a GA4 Measurement Protocol API secret?

In GA4 go to Admin > Data streams, open your stream and choose Measurement Protocol API secrets. Create a new secret, update your server to use it, then delete the old one.

Why is my GA4 showing fake purchases or conversions I can't explain?

One possible cause is that someone is sending events through the Measurement Protocol, which needs only your Measurement ID and API secret. If the secret is in your frontend code, rotate it and move the calls to the server. Other causes include bot traffic and duplicate tags.

Can I delete fake events from GA4 after they were collected?

No, GA4 does not let you delete collected event data in standard reports. You can filter or segment it out in analysis, and stop new fake data by rotating the secret. Note the date range affected so you can explain gaps in reports.

Do I need the API secret for normal browser tracking with gtag.js?

No. The standard GA4 tag only needs the Measurement ID. The API secret is only for Measurement Protocol calls sent from a server or app, so a normal browser setup should never include it.

Does FunnelGuard detect an exposed Measurement Protocol secret?

Not yet. It is a possible future check. FunnelGuard currently checks journeys, tracking hits and test lead delivery, so for this issue you should search your page source and review your GA4 secrets manually.

Does FunnelGuard catch this?

Could add

Not checked yet: could be added to FunnelGuard.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.