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.
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.
- Secret in page codeapi_secret is readable in source or JS file
- Copied by a third partyBot, competitor or curious visitor
- Fake events sentFake purchases and leads reach GA4
- 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.
Example: a month where fake Measurement Protocol events inflate reported conversions. Numbers are illustrative only.
How to check if your site has it
Search the page source
Open your key pages, press Ctrl+U (Cmd+Option+U on Mac) and search for
api_secretandmp/collect.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.Check Google Tag ManagerGoogle Tag Manager
In your container, use the search box for
api_secretandmp/collect. Look at Custom HTML tags and variables.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.
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
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.
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.
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.
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.
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.
// 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 }]
})
});
}Check plugins and theme
Search
wp-content/themesandwp-content/pluginsforapi_secret. Review analytics plugin settings and any code snippet plugin.Move the call to PHP
Call the endpoint with
wp_remote_post()in a server hook, and define the secret inwp-config.phprather than printing it in a template.
Check theme and pixels
In Online Store > Themes > Edit code, search for
api_secret. Also check Settings > Customer events for custom pixels that contain it.Use a server or app
Send events from a backend that receives Shopify webhooks, such as the order paid webhook, so the secret never reaches the storefront.
Find tags and variables
Search the container for
api_secret. Check Custom HTML tags, constants and JavaScript variables, then delete or pause them and publish.Consider a server container
If you use a server-side GTM container, keep the secret in the server environment and send the event from there.
Check custom code
Look in Project settings > Custom code and in page-level custom code and embeds for
api_secret. Remove it and publish.Use an external backend
Webflow does not run your server code, so send events from a separate backend or automation that holds the secret.
Check tracking code
Review the custom code areas in funnels and websites, plus any custom JavaScript on forms, for
api_secret. Remove it.Use a private endpoint
Send events from a server you control, triggered by a workflow webhook, so the secret is not in the page.
Check custom code
In Settings > Custom code, and in any embed element, search for
api_secretand remove it.Use backend code
If you use Velo, call the endpoint from backend
.web.jsor.jswfiles and keep the secret in the Secrets Manager.
Check code injection
In Settings > Advanced > Code injection, and in code blocks, search for
api_secretand remove it.Use an external backend
Squarespace has no server code, so run Measurement Protocol calls from a separate service that holds the secret.
Search the repository
Search the codebase and build output for
api_secret. Check bundled JS, source maps and.envfiles that are served publicly.Use environment variables
Read the secret from server environment variables, and make sure frontend build tools do not inline it (for example variables with a public prefix).
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
- Searching page source and loaded JS for
api_secretandmp/collectfinds nothing - Tag Manager search for
api_secretreturns 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.
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
- Keep all secrets in environment variables or a secrets manager
- Add a code review rule: no
api_secretin 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.
Day 0
Developer adds a client-side Measurement Protocol call with the secret inline.
Week 2
A bot or visitor copies the secret from the source.
Week 3
Fake events start to appear; conversion numbers rise without more revenue.
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.
Related issues
- TRK-350Ghost/referral spam hits via Measurement ProtocolFix guide
- TRK-345Bot/spam traffic inflating sessionsFix guide
- TRK-346Spam form submissions firing lead conversionsFix guide
- TRK-349Events from localhost or staging domains in production propertyFix guide
- TRK-344Test orders/leads counted as real conversionsFix guide
- TRK-338Duplicate purchase across browser and server without dedupFix guide