|
| 1 | +{% set property = config.google_analytics %} |
| 2 | +<!-- Google Analytics 4 (G-XXXXXXXXXX) --> |
| 3 | +<script> |
| 4 | + /* Use GA when hosted by our public docs site, not when installed. */ |
| 5 | + if (location.href.indexOf("docs.posit.co") >= 0) { |
| 6 | + window.dataLayer = window.dataLayer || [] |
| 7 | + function gtag() { dataLayer.push(arguments) } |
| 8 | + /* Set up integration and send page view */ |
| 9 | + gtag("js", new Date()) |
| 10 | + gtag("config", "{{ property[0] }}") |
| 11 | + |
| 12 | + /* Register virtual event handlers */ |
| 13 | + document.addEventListener("DOMContentLoaded", function () { |
| 14 | + if (document.forms.search) { |
| 15 | + var query = document.forms.search.query |
| 16 | + query.addEventListener("blur", function () { |
| 17 | + if (this.value) { |
| 18 | + gtag("event", "search", { search_term: this.value }) |
| 19 | + } |
| 20 | + }) |
| 21 | + } |
| 22 | + |
| 23 | + /* Send page view on location change */ |
| 24 | + if (typeof location$ !== "undefined") |
| 25 | + location$.subscribe(function (url) { |
| 26 | + gtag("config", "{{ property[0] }}", { |
| 27 | + page_path: url.pathname |
| 28 | + }) |
| 29 | + }) |
| 30 | + }) |
| 31 | + /* Create script tag */ |
| 32 | + var script = document.createElement("script") |
| 33 | + script.async = true |
| 34 | + script.src = "https://www.googletagmanager.com/gtag/js?id={{ property[0] }}" |
| 35 | + |
| 36 | + /* Inject script tag */ |
| 37 | + var container = document.getElementsByTagName("head")[0] |
| 38 | + var firstChild = container.firstChild |
| 39 | + container.insertBefore(script, firstChild) |
| 40 | + } |
| 41 | +</script> |
0 commit comments