-
Notifications
You must be signed in to change notification settings - Fork 0
Google Analytics: enable #22
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
Changes from all commits
ec5597b
a052337
9d64cbb
f919178
f4f7914
980fc8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {% if site.analytics.google.tracking_id %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <script async src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.tracking_id }}"></script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| window.dataLayer = window.dataLayer || []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function gtag() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataLayer.push(arguments); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gtag('js', new Date()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gtag('config', '{{ site.analytics.google.tracking_id }}', { 'anonymize_ip': true }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gtag('config', '{{ site.analytics.google.tracking_id }}', { 'anonymize_ip': true }); | |
| gtag('config', '{{ site.analytics.google.tracking_id }}', { 'anonymize_ip': {{ site.analytics.google.anonymize_ip }} }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line hardcodes the anonymize_ip option. The theme's built-in implementation handles this more flexibly by checking the site.analytics.google.anonymize_ip setting from _config.yml. This makes the theme's version more maintainable, as configuration changes only need to be made in one place. This is another reason to prefer the theme's native analytics integration over a custom implementation.
gtag('config', '{{ site.analytics.google.tracking_id }}'{% if site.analytics.google.anonymize_ip %}, { 'anonymize_ip': true }{% endif %});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This analytics script can be improved in two ways:
- Respect
anonymize_ipsetting: Your_config.ymlhasanonymize_ip: true, but this script doesn't apply it. This is important for user privacy. - Add robustness check: The script should only be rendered if a
tracking_idis present in the configuration to avoid errors.
The suggested code below addresses both points by wrapping the script in a conditional and adding the anonymize_ip parameter.
| <script async src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.tracking_id }}"></script> | |
| <script> | |
| window.dataLayer = window.dataLayer || []; | |
| function gtag() { | |
| dataLayer.push(arguments); | |
| } | |
| gtag('js', new Date()); | |
| gtag('config', '{{ site.analytics.google.tracking_id }}'); | |
| </script> | |
| {% if site.analytics.google.tracking_id %} | |
| <script async src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.tracking_id }}"></script> | |
| <script> | |
| window.dataLayer = window.dataLayer || []; | |
| function gtag() { | |
| dataLayer.push(arguments); | |
| } | |
| gtag('js', new Date()); | |
| gtag('config', '{{ site.analytics.google.tracking_id }}'{% if site.analytics.google.anonymize_ip %}, { 'anonymize_ip': true }{% endif %}); | |
| </script> | |
| {% endif %} |
Copilot
AI
Dec 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Google Analytics tracking code is being included unconditionally without checking if analytics is enabled or if a tracking ID is configured. Consider wrapping the analytics code in a conditional check to only load when the tracking_id is present. This prevents loading tracking scripts unnecessarily and improves privacy.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {% include google-analytics.html %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation: The function declaration uses spaces for indentation while the rest of the script uses tabs. This should be standardized to match the project's coding style. Either use all tabs or all spaces throughout the script.