Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add google analytics 4 support #74

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ titles_from_headings:
# - languages/index.md
# - process/speclet_playbook.md
# - process/testing_strategy.md


google_analytics: G-2NX5YFW46E
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assume this is ok to be in the repo?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see what's going on below. so do we want the value in the repo or 🤷

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed BY_DESIGN approved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, for future reference this is not a secret. It'll show up many times in the network tab as part of URLs. It's just the way GA works. If someone else wants to steal it, they're welcome to enjoy their console.log'd wrong domain messages courtesy of GA.

17 changes: 17 additions & 0 deletions _includes/body/analytics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if site.google_analytics %}
<script>!function (w, d) {
/* this file can be removed once GA4 support is added upstream in hydejack */
/* See: https://github.com/hydecorp/hydejack/issues/310 */
/* This is a variation of the code in PR330 on hydejack */


window.dataLayer = w.dataLayer || [];
function gtag() {dataLayer.push(arguments);}
gtag('js', new Date());

/* start tracking, will respect consent if set */
gtag('config', '{{ site.google_analytics }}');

w.loadJSDeferred('https://www.googletagmanager.com/gtag/js?id={{ site.google_analytics }}');
}(window, document);</script>
{% endif %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a combination of liquid/jekyll code wrapping JS here. The JS is by and large what Google suggests just with the necessary Jekyll/theme stuff merged in.

Screen Shot 2023-09-15 at 9 40 44 AM