A Service Worker is a script that can intercept network requests, store data temporarily, and retry sending it later if the internet connection is lost.
Google introduced service workers for GTM Server Side Tagging to improve data collection and reliability.
Before service workers, GTM tags use methods like navigator.sendBeacon() to send tracking data (events) directly to Google Analytics, Ads or other platforms.
The problem? Some data could be lost if the user closes the page too quickly or loses internet connection.
A Service Worker can catch the tracking requests, store them temporarily, and send them later when the internet connection is back. This ensures more complete data collection, reducing the risk of missing events due to bad connectivity or fast exits.
If the user goes offline, the Service Worker can send data once they are back online.
Who exactly uses the Service Worker in GTM Server-Side Tagging?
Google’s Tracking Scripts (gtag.js, GTM, GA, or Ads Scripts) use the Service Worker to enhance data collection and measurement reliability.
Who deploys the Service Worker in GTM Server-Side Tagging?
If Google has implemented a Service Worker as part of its Google Tag (gtag.js) or GTM scripts, then Google itself is deploying and managing it.
If a Service Worker is part of Google’s tracking ecosystem, it is loaded dynamically by Google’s scripts, not something website owners explicitly install.
sGTM itself does not deploy or require a Service Worker to function, but it can benefit from one if Google’s tracking scripts implement it.
If a Service Worker is used, it modifies requests before they reach sGTM, ensuring that tracking data is more reliable.
A website owner would only deploy their own Service Worker if they were customising tracking beyond Google’s default implementation.
The presence of a Service Worker does not require any action from the website owner unless they wish to modify default behavior.
The website owner only interacts with the Service Worker indirectly by integrating GTM Server-Side, not by manually setting it up.
The only direct action a website owner might need to take regarding Service Workers in GTM Server-Side Tagging is updating their Content Security Policy (CSP) if it is too restrictive.
If a website has a strict CSP, it might block the registration or execution of the Service Worker. This can prevent the Service Worker from intercepting and handling GTM tracking requests.
Open your browser’s Developer Tools (F12 or right-click, select “Inspect”), and go to the Console tab. Look for any CSP-related error messages that might indicate the Service Worker is being blocked.
The name of the Service Worker script.
The name of the Service Worker script in GTM (or any implementation) is not fixed and depends on how Google has implemented it.
Common filenames include:
‘sw.js’,
‘gtm-sw.js’,
‘gtag-sw.js’,
‘google-tag-sw.js’,
‘/_/service_worker/xyz/sw.js’ (dynamic naming by Google).
How to check if Service Worker is being used on your website?
You can check if a Service Worker is being used on your website by following the steps below:
Step-1: Open a website that uses GTM Server-Side Tagging in Google Chrome.
Step-2: Right-click anywhere on the page and select “Inspect“.
Step-3: In Chrome DevTools, find and click on the “Application” tab.
Step-4: Under ‘Service Workers’, check if any script is registered by looking at the source file (e.g., sw.js, gtm-sw.js, gtag-sw.js, or another name).
If present, this confirms that your Google Tag is using a Service Worker to handle tracking data. If a Service Worker is registered, then proceed to the next step.
Step 5: Click on ‘Network requests’ button to see all the requests initiated by the Service Worker.
Step-6: Click on ‘Update’ to manually trigger an update of the service worker.
This action forces the browser to check for a new version of the service worker script, bypassing the usual update interval.
Step-7: Click on ‘Unregister’ to manually unregister the service worker.
This action will immediately stop the service worker from controlling the page. The service worker is removed from the browser’s registry. Any caches associated with that service worker are deleted.
This feature is particularly useful for developers and testers who want to:
Reset the service worker state during development.
Test how the site behaves without the service worker.
Troubleshoot issues related to service worker caching or offline functionality.
Note: After unregistering, the page will need to be reloaded for the changes to take effect fully. If the service worker is set to register automatically on page load, it may re-register unless you’ve also disabled that functionality in your code.
Step-8: Click on ‘See all registrations’ link to inspect service workers registrations across all origins that are currently active in the browser:
It provides a comprehensive overview of all service worker activity in the browser, which can be crucial for understanding complex web applications or troubleshooting service worker-related issues.
Step-9: Navigate to the ‘Network‘ tab to verify if the service worker is intercepting the tracking request.
Step-10: In the Filter bar, type is:service-worker-initiated. This filter shows requests initiated by a Service Worker.
Step-11: Click on a tracking request.
Step-12: Open the ‘Headers’ tab and look at the request URL:
Step-13: Look for ‘service-worker-allowed: /_/service_worker‘ under the response header. If present, it confirms that GTM’s Service Worker is allowed and active.
Step-14 (optional):
Navigate to your GTM server side tagging domain (e.g., sgtm.example.com).
Open the Console tab in DevTools and run the following command: navigator.serviceWorker.getRegistrations().then(console.log)
If the command returns the following similar object, a Service Worker is registered and actively controlling this page.
If the command returns an empty array [], it means the Service Worker is registered under a different subdomain (may be ‘www.example.com’).
Note: In GTM Server-Side Tagging, the Service Worker is most likely registered on the server-side tagging domain (e.g., sgtm. example. com), not on the main website (e.g., www.example.com).
Can the service worker cause a loss in data collection?
Yes, if the service worker is not correctly registered or fails to activate, it will not intercept requests as intended.
This could happen if:
The browser blocks Service Worker registration due to strict security settings.
The scope of the Service Worker does not cover the tracking events.
Users disable JavaScript or block Service Workers using browser extensions.
If a user clears their cache, the Service Worker may be removed from the browser, causing it to lose any pending tracking events that were queued for resending.
Note: Google has not provided full details on how it uses Service Workers, so monitoring your network requests for any unexpected behavior is a good practice.
Clarification about the navigator.serviceWorker.getRegistrations() Command.
This command only shows Service Workers registered for the current origin and scope.
If the Service Worker is registered under a different subdomain, this command returns an empty array even if a Service Worker is actually in use.
If the scope of the Service Worker does not cover the current page, the command will not detect it.
A Service Worker can still process tracking requests (e.g., via fetch events) without being “active” in the way getRegistrations() checks for.
Cross-Origin Service Workers Won’t Appear in Console Commands.
Checking the Console logs with navigator.serviceWorker.getRegistrations() is NOT a 100% reliable way to confirm whether a Service Worker is being used for tracking.
My best selling books on Digital Analytics and Conversion Optimization
Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.
Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.
Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.
Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.
About the Author
Himanshu Sharma
Founder, OptimizeSmart.com
Over 15 years of experience in digital analytics and marketing
Author of four best-selling books on digital analytics and conversion optimization
Nominated for Digital Analytics Association Awards for Excellence
Runs one of the most popular blogs in the world on digital analytics
Consultant to countless small and big businesses over the decade
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.