Skip to content

Commit

Permalink
Updated Google Analytics to support GA4 / google tag manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jabelone committed Oct 5, 2023
1 parent 0a3b207 commit ffca9e5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/POST_INSTALL_STEPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ However, as noted below, currencies will use a hardcoded value set by a configur
### General
* "SITE_NAME" - Name of the website.
* "SITE_OWNER" - Name of the organisation running this website.
* "GOOGLE_ANALYTICS_PROPERTY_ID" - Enter your Google Analytics Tracking ID to enable Google Analytics Tracking.
* "GOOGLE_ANALYTICS_MEASUREMENT_ID" - Enter your measurement ID to enable Google analytics. Only the new GA4 measurement IDs are supported. It should look something like G-XXXXXXXXXX.
* "API_SECRET_KEY" - Secret key used to authenticate some requests from access control devices.

### Signup
Expand Down
2 changes: 1 addition & 1 deletion memberportal/api_general/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def get(self, request):
"webcamLinks": webcam_links,
"keys": keys,
"features": features,
"analyticsId": config.GOOGLE_ANALYTICS_PROPERTY_ID,
"analyticsId": config.GOOGLE_ANALYTICS_MEASUREMENT_ID,
"sentryDSN": config.SENTRY_DSN_FRONTEND,
}

Expand Down
6 changes: 3 additions & 3 deletions memberportal/membermatters/constance_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@
"",
"Secret key for the discourse SSO protocol (if enabled).",
),
"GOOGLE_ANALYTICS_PROPERTY_ID": (
"GOOGLE_ANALYTICS_MEASUREMENT_ID": (
"",
"Place you google analytics property ID here to enable Google analytics integration.",
"Enter your measurement ID to enable Google analytics. Only the new GA4 measurement IDs are supported. It should look something like G-XXXXXXXXXX.",
),
"API_SECRET_KEY": (
"PLEASE_CHANGE_ME",
Expand Down Expand Up @@ -343,7 +343,7 @@
"SITE_NAME",
"SITE_OWNER",
"SITE_LOCALE_CURRENCY",
"GOOGLE_ANALYTICS_PROPERTY_ID",
"GOOGLE_ANALYTICS_MEASUREMENT_ID",
"API_SECRET_KEY",
"SITE_BANNER",
),
Expand Down
9 changes: 0 additions & 9 deletions src-frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
/>

<script src="https://js.stripe.com/v3/"></script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
<script>
window.ga =
window.ga ||
function () {
(ga.q = ga.q || []).push(arguments);
};
ga.l = +new Date();
</script>

<link
rel="icon"
Expand Down
18 changes: 16 additions & 2 deletions src-frontend/src/store/modules/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,22 @@ export default {
const { analyticsId } = result.data;

if (analyticsId) {
ga('create', analyticsId, 'auto');
ga('send', 'pageview');
const scriptId = 'gtag-script';
if (!document.getElementById(scriptId)) {
const script = document.createElement('script');
script.id = scriptId;
script.async = true;
script.src = `https://www.googletagmanager.com/gtag/js?id=${analyticsId}`;
document.head.insertBefore(script, document.head.firstChild);

window.dataLayer = window.dataLayer || [];
window.gtag = function () {
dataLayer.push(arguments);
};

gtag('js', new Date());
gtag('config', analyticsId);
}
}

resolve();
Expand Down

0 comments on commit ffca9e5

Please sign in to comment.