Skip to content

Commit 0eec656

Browse files
update to GA4
1 parent 96ca871 commit 0eec656

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

docs/mkdocs.yml

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
site_name: 'Posit Connect: rsconnect-python'
22
copyright: Posit Software, PBC. All Rights Reserved
33

4+
# We activate GA only when hosted on our public docs site
5+
# and not when installed.
6+
#
7+
# See overrides/partials/integrations/analytics.html
8+
google_analytics:
9+
- 'GTM-KHBDBW7'
10+
- 'auto'
11+
412
markdown_extensions:
513
- toc:
614
permalink: "#"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)