-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
d0557a7
commit 0a5ae7c
Showing
5 changed files
with
127 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = [ | ||
|