← Back to blog

Cloudflare Zaraz or your own Worker: server-side GA4 analytics without gtag.js

· Technical analysis

Imagine this: a potential client reads your services page on an iPhone on Sunday, but submits an enquiry only eight days later. In your Google Analytics report, they are already a new person who has come “from nowhere”.

This happens because of Apple’s ITP (Intelligent Tracking Prevention) [1], while ad blockers hide some visitors altogether. If your website already runs through Cloudflare, there are three ways to deal with this. In this article we compare them, from the quickest to the most reliable.

Short answer

  • Google Tag Gateway – one click, but it does not solve the Safari problem. We rule it out.
  • Cloudflare Zaraz – a good, quick, no-code solution. You can set it up yourself in 15 minutes.
  • Your own Cloudflare Worker – the most reliable: a long-lived cookie in Safari, absent from ad blocker lists, consent checked on the server. It needs code and maintenance.

1. Why does GA4 “lose” visitors?

Safari ITP restricts cookies created by JavaScript (document.cookie): they last no longer than 7 days [1], and if the visitor arrives via a decorated advertising link (e.g. ?gclid=), only 24 hours [2]. The standard gtag.js creates its _ga cookie in exactly this way. Since Safari 16.4, the 7-day limit also applies to cookies set by the server if the server’s IP does not match the website’s IP [3]. ITP affects all browsers on iPhone and iPad, because they use WebKit.

Ad blockers block requests to google-analytics.com and googletagmanager.com. In a technical audience (IT, developers, security specialists) the share of blocker users is high, so the loss is especially noticeable on B2B websites.

The rule is simple: only a cookie set by the server through the Set-Cookie HTTP header, from a server with the same IP as the website, can be long-lived, and to keep blockers out of the way, requests must go to your own domain and stay off the blocklists.

2. Google Tag Gateway: quick, but not enough

Google Tag Gateway is a free Cloudflare feature: gtag.js and measurement requests travel through a path on your domain (e.g. /okp0), and Cloudflare forwards them to Google [4]. It is enabled with one click and reduces the impact of some blockers.

However, gtag.js remains in the browser, and the _ga cookie is still written by JavaScript. This means Safari’s 7-day limit does not go anywhere. We tried this option ourselves and ruled it out: it solves only half the problem.

3. Cloudflare Zaraz: a good solution you can set up yourself

Zaraz is Cloudflare’s tag management tool, which runs third-party tools (GA4, Google Ads, Meta, LinkedIn and others [16]) on Cloudflare’s servers rather than in the visitor’s browser. The page keeps only a small /cdn-cgi/zaraz/i.js script served from your domain, and gtag.js is no longer loaded.

Why is Zaraz worth considering?

  • Quick and no code. Everything is configured in the Cloudflare dashboard.
  • Free. 1,000,000 events per month per account, then USD 5 per million [5]. For a small or medium-sized website that is more than enough.
  • Events without a developer. Clicks, scroll depth, form submissions, element visibility and timers are set up in the dashboard [6].
  • Consent management. A built-in consent dialog, IAB TCF support [7] and Google Consent Mode v2 [8].
  • All pixels in one place. If you advertise on several platforms, this is a big advantage.

How to set it up in 15 minutes

  1. Remove the gtag.js / GTM code from the website (otherwise the data will be duplicated).
  2. In the Cloudflare dashboard, open Zaraz → Tools configuration → Add new tool → Google Analytics 4 and enter the measurement ID (G-…).
  3. Under Zaraz → Consent, enable the consent dialog and assign GA4 to the analytics purpose.
  4. For the enquiry form, create a Form submission trigger and send a generate_lead event (or in code: zaraz.track('generate_lead')).
  5. Check the GA4 Realtime report.

What should you know before choosing?

  • Ad blockers. The EasyPrivacy list (version of 2026-10-15) contains the rule /cdn-cgi/zaraz/* [10]. uBlock Origin uses this list by default, so such visitors do not see Zaraz, even though requests go through your domain.
  • Safari cookies. Cloudflare does not document how Zaraz sets its cookies or whether the ITP limit applies to them. Part of the state is stored in localStorage, to which ITP also applies the 7-day rule. If returning Safari visitors matter to you, check for yourself.
  • IP address. By default, Zaraz passes the visitor’s full IP to Google. With Hide Originating IP enabled, GA4 determines location from the Cloudflare server’s IP, which may be inaccurate [9].
  • Engaged sessions. In the open-source GA4 component code, a session is marked as engaged only when all three conditions are met (10 s, an enquiry and 2 page views) [11], whereas the GA4 definition requires only any of them [12]. We did not measure how much this affects reports.
  • Development. The latest entry in the Zaraz changelog is from February 2025 [14]. The product works and is documented, but new features appear rarely.

Conclusion: if you want to do it quickly and yourself, and Safari or blocker losses are not critical, Zaraz is a better choice than ordinary gtag.js or Tag Gateway.

4. A trap when building your own solution: the Measurement Protocol

It is natural to assume that your own server needs the official GA4 Measurement Protocol (MP). That is how we started too. After a day, all new sessions in the GA4 reports were Unassigned, with 0 new users and 0 engaged sessions.

The cause is in Google’s own documentation: MP is designed to supplement gtag.js data (for example, a payment captured on the server), not to replace it [13]. GA4 calculates traffic source, new users and engagement only from ordinary gtag.js requests. So a working server-side solution sends the same requests as gtag.js (to /g/collect), which is what both Zaraz and server-side Google Tag Manager do.

5. Your own Cloudflare Worker: the most reliable option

The idea: only a few kilobytes of your own JavaScript remain in the browser, which sends an event to your endpoint. A Worker running on the same domain sets the cookies and forwards the data to GA4.

Browser                          Your domain (Cloudflare)                Google
analytics.js ── POST /api/metrics ──► Worker
 (no gtag.js)                          ├─ no consent? ──► 204, nothing stored
                                       ├─ Set-Cookie: __av_cid (400 d., HttpOnly)
                                       ├─ Set-Cookie: __av_sid (30 min., HttpOnly)
                                       └─ fetch ───────────────────────────► GA4 /g/collect
  • Safari visitors are recognised for up to 400 days. The cookie is set by the server through an HTTP header, on the same IP as the website, so the ITP limit does not apply. HttpOnly means no script can overwrite it.
  • Not on blocker lists. You choose the endpoint path yourself. Avoid generic paths: EasyPrivacy already blocks, for example, /api/metrics/collect [10].
  • Consent is checked on the server. Without analytics consent, the Worker sets no cookies and sends nothing to Google, even if the browser code goes wrong.
  • The IP is not passed on. Google receives 0.0.0.0. If you need the country, a truncated IP can be sent (the last octet is cleared).
  • Normal GA4 reports. Sources, new users, landing pages and engaged sessions according to the GA4 definition [12].
  • A clean page. No requests to google* domains; a connect-src 'self' CSP rule is enough.

The cost: this is code you have to maintain. /g/collect is an undocumented address (gtag.js itself relies on it, so sudden changes are unlikely). Events (e.g. generate_lead) have to be defined in code, and advertising pixels need a separate solution.

We have published our solution as an open-source project, avbdar (MIT licence) [15]: the Worker, browser client, CookieConsent dialog (Lithuanian and English), tests and documentation. We use it on this website too.

6. Comparison

Tag GatewayZarazOwn Worker
Setup1 click~15 min., no codeCode and configuration
gtag.js in the browserYesNoNo
Safari cookie lifetime7 d. (24 h from an ad)Not documentedUp to 400 d.
EasyPrivacy / uBlock OriginPartlyBlockedNot blocked
Visitor IP to GoogleAs configuredPassed on by defaultNot passed on
Consent checkIn the browserZaraz CMP, TCF, Consent ModeOn the server
Events without codeVia GTMYesNo
Advertising pixelsGoogleMany platformsGA4 only
CostFree1 million events/month freeFree (Workers Free)
MaintenanceGoogle / CloudflareCloudflareYou or a partner

7. Which should you choose?

  • Zaraz – if you want to sort it out quickly yourself, have no developer, advertise on several platforms and can live with some Safari and blocker losses.
  • Your own Worker – if the buying decision takes more than a week (B2B, higher-priced services), your audience is technical, it matters to see exactly where enquiries come from, and you want maximum control over privacy.
  • Both together – GA4 through your own Worker and advertising pixels through Zaraz. Just do not enable GA4 in both places.

Do it yourself or delegate it to us

Code and instructions – avbdar on GitHub. If you would like us to do it for you: we will deploy the Worker in your Cloudflare zone, configure consent and GA4 (key events, enquiry forms), and verify the result in Safari.

Book a consultation

A common question: “if the data goes through my server, can I collect it without a cookie notice?” No. Under the GDPR and Article 61 of the Lithuanian Law on Electronic Communications, storing non-essential cookies or other identifiers on a device requires prior, informed and active consent, regardless of whether the data travels through the browser or through a server. This applies to Zaraz and to your own Worker alike. In your privacy policy, list the cookies and describe the transfer of data to Google.

Frequently asked questions

Is Cloudflare Zaraz free?

Every Cloudflare account gets 1,000,000 free Zaraz events per month. Above that limit, each additional million costs USD 5 per month [5].

Why does GA4 show Unassigned?

The most common cause in server-side setups is that events are sent only through the Measurement Protocol, which is designed to supplement gtag.js, not replace it [13]. See section 4.

Can Zaraz and your own Worker be used together?

Yes. Send GA4 through your own Worker and manage advertising pixels through Zaraz. The important thing is that GA4 is not enabled in both places, otherwise the data will be duplicated.

Does your own Worker cost anything?

For small and medium-sized websites, no: the free Cloudflare Workers plan allows 100,000 requests per day. The main cost is the time needed to implement and maintain it.

Sources

  1. WebKit.org — Intelligent Tracking Prevention 2.1 (7-day cookie limit)
  2. WebKit.org — Intelligent Tracking Prevention 2.2 (24-hour limit)
  3. WebKit.org — Tracking Prevention in WebKit
  4. Cloudflare Docs — Google tag gateway for advertisers
  5. Cloudflare Docs — Zaraz pricing
  6. Cloudflare Docs — Zaraz triggers and rules
  7. Cloudflare Docs — Zaraz consent management
  8. Cloudflare Docs — Zaraz Google Consent Mode
  9. Cloudflare Docs — Zaraz FAQ (Hide Originating IP)
  10. EasyList — EasyPrivacy filter list (version 202610151533)
  11. GitHub — Zaraz GA4 managed component, requestBuilder.ts
  12. Google Analytics Help — Engagement rate and bounce rate
  13. Google for Developers — Measurement Protocol (Google Analytics 4)
  14. Cloudflare Docs — Zaraz changelog
  15. GitHub — avbdar: server-side GA4 analytics with Cloudflare Workers
  16. Cloudflare Docs — Zaraz supported tools

Need help with your Cloudflare and analytics configuration?

We will review your Cloudflare zone, analytics implementation, consent management and Safari cookie lifetime. We will tell you what you can fix yourself and where it is worth getting help.

Book a consultation