Skip to content

Instantly share code, notes, and snippets.

@spirinvladimir
Created July 6, 2026 14:41
Show Gist options
  • Select an option

  • Save spirinvladimir/46b62226330a66795800026044f8a85e to your computer and use it in GitHub Desktop.

Select an option

Save spirinvladimir/46b62226330a66795800026044f8a85e to your computer and use it in GitHub Desktop.
Keep Google Analytics 4 at CacheStorage and loading with priority low
const GA = 'https://www.googletagmanager.com/gtag/js?id=GT-TQL9TSPB'
caches.open('ga')
.then(c =>
c.match(GA)
.then(r =>
r || fetch(GA, {priority: 'low'})
.then(r =>
r.ok && c.put(GA, r.clone()).then(_ => r)
)
)
)
.then(r => r.text())
.then(code => {
const script = document.createElement('script')
script.textContent = code
document.head.appendChild(script)
window.dataLayer = window.dataLayer || []
window.gtag = function gtag(){dataLayer.push(arguments)}
gtag('js', new Date())
gtag('config', 'GT-TQL9TSPB')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment