Skip to content

Commit

Permalink
Feat/skip ga for cds users (#1683)
Browse files Browse the repository at this point in the history
* feat(skip GA): don't load GA scripts for cds users

* test(google_analytics): ensure js is loaded for logged out and non-cds users, and not loaded for cds users

* chore: fix test comments

* fix(google analytics): only load in prod environment
  • Loading branch information
andrewleith authored Oct 11, 2023
1 parent d0557a7 commit 0a5ae7c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 27 deletions.
32 changes: 17 additions & 15 deletions app/templates/partials/google-analytics.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
nonce="{{ request_nonce }}"
src="https://www.googletagmanager.com/gtag/js?id={{ google_analytics_id }}"
></script>
<script nonce="{{ request_nonce }}">
<!-- The data layer: https://developers.google.com/tag-platform/tag-manager/web/datalayer -->
window.dataLayer = window.dataLayer || []
{% if config["NOTIFY_ENVIRONMENT"].lower() == 'production' and current_user.email_domain != 'cds-snc.ca' %}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
nonce="{{ request_nonce }}"
src="https://www.googletagmanager.com/gtag/js?id={{ google_analytics_id }}"
></script>
<script nonce="{{ request_nonce }}">
<!-- The data layer: https://developers.google.com/tag-platform/tag-manager/web/datalayer -->
window.dataLayer = window.dataLayer || []

function gtag() {
dataLayer.push(arguments)
}
function gtag() {
dataLayer.push(arguments)
}

gtag("js", new Date())
gtag("config", "{{ google_analytics_id }}", {anonymize_ip: true})
</script>
gtag("js", new Date())
gtag("config", "{{ google_analytics_id }}", {anonymize_ip: true})
</script>
{% endif %}
10 changes: 6 additions & 4 deletions app/templates/partials/google-tag-manager-body.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ google_tag_manager_id }}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
{% if config["NOTIFY_ENVIRONMENT"].lower() == 'production' and current_user.email_domain != 'cds-snc.ca' %}
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ google_tag_manager_id }}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
{% endif %}
18 changes: 10 additions & 8 deletions app/templates/partials/google-tag-manager-head.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<!-- Google Tag Manager -->
<script nonce='{{ request_nonce }}'>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{{ google_tag_manager_id }}');</script>
<!-- End Google Tag Manager -->
{% if config["NOTIFY_ENVIRONMENT"].lower() == 'production' and current_user.email_domain != 'cds-snc.ca' %}
<!-- Google Tag Manager -->
<script nonce='{{ request_nonce }}'>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{{ google_tag_manager_id }}');</script>
<!-- End Google Tag Manager -->
{% endif %}
61 changes: 61 additions & 0 deletions tests/app/main/views/test_google_analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from tests.conftest import set_config


def test_google_analytics_logged_in_staging(client_request, active_cds_user_with_permissions, active_user_with_permissions, app_):
with set_config(app_, "NOTIFY_ENVIRONMENT", "staging"):
# Ensure GA scripts ARE NOT loaded for cds users
client_request.login(active_cds_user_with_permissions)
page = client_request.get("main.welcome")
assert "googletagmanager" not in str(page.contents)

# Ensure GA scripts ARE NOT loaded for non-cds users
client_request.login(active_user_with_permissions)
page = client_request.get("main.welcome")
assert "googletagmanager" not in str(page.contents)


def test_google_analytics_logged_out_staging(
client_request, active_cds_user_with_permissions, active_user_with_permissions, app_
):
with set_config(app_, "NOTIFY_ENVIRONMENT", "staging"):
# Ensure GA scripts ARE NOT loaded for logged out users
client_request.login(active_cds_user_with_permissions)
client_request.logout()
page = client_request.get("main.sign_in")
assert "googletagmanager" not in str(page.contents)

client_request.login(active_user_with_permissions)
client_request.logout()
page = client_request.get("main.sign_in")
assert "googletagmanager" not in str(page.contents)


def test_google_analytics_logged_in_production(
client_request, active_cds_user_with_permissions, active_user_with_permissions, app_
):
with set_config(app_, "NOTIFY_ENVIRONMENT", "production"):
# Ensure GA scripts ARE NOT loaded for cds users
client_request.login(active_cds_user_with_permissions)
page = client_request.get("main.welcome")
assert "googletagmanager" not in str(page.contents)

# Ensure GA scripts ARE loaded for non-cds users
client_request.login(active_user_with_permissions)
page = client_request.get("main.welcome")
assert "googletagmanager" in str(page.contents)


def test_google_analytics_logged_out_production(
client_request, active_cds_user_with_permissions, active_user_with_permissions, app_
):
with set_config(app_, "NOTIFY_ENVIRONMENT", "production"):
# Ensure GA scripts are loaded for logged out users
client_request.login(active_cds_user_with_permissions)
client_request.logout()
page = client_request.get("main.sign_in")
assert "googletagmanager" in str(page.contents)

client_request.login(active_user_with_permissions)
client_request.logout()
page = client_request.get("main.sign_in")
assert "googletagmanager" in str(page.contents)
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,39 @@ def active_user_with_permissions(fake_uuid):
return user_data


@pytest.fixture(scope="function")
def active_cds_user_with_permissions(fake_uuid):
user_data = {
"id": fake_uuid,
"name": "Test User",
"password": "somepassword",
"password_changed_at": str(datetime.utcnow()),
"email_address": "[email protected]",
"mobile_number": "6502532222",
"blocked": False,
"state": "active",
"failed_login_count": 0,
"permissions": {
SERVICE_ONE_ID: [
"send_texts",
"send_emails",
"send_letters",
"manage_users",
"manage_templates",
"manage_settings",
"manage_api_keys",
"view_activity",
]
},
"platform_admin": False,
"auth_type": "sms_auth",
"organisations": [ORGANISATION_ID],
"services": [SERVICE_ONE_ID],
"current_session_id": None,
}
return user_data


@pytest.fixture(scope="function")
def active_user_with_permission_to_two_services(fake_uuid):
permissions = [
Expand Down

0 comments on commit 0a5ae7c

Please sign in to comment.