From 80c75d9806acb6509196b4e1f07d55b4f3b00b8d Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 28 Sep 2020 19:04:09 +0200 Subject: [PATCH] feat: option to anonymize ip for google analytics --- frappe/data/sample_site_config.json | 3 +++ .../includes/app_analytics/google_analytics.html | 3 +++ .../doctype/website_settings/website_settings.json | 12 +++++++++--- frappe/www/desk.py | 1 + frappe/www/website_script.js | 3 +++ frappe/www/website_script.py | 10 ++++++++-- 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/frappe/data/sample_site_config.json b/frappe/data/sample_site_config.json index 36818ef286..715cd7b9fa 100644 --- a/frappe/data/sample_site_config.json +++ b/frappe/data/sample_site_config.json @@ -22,6 +22,9 @@ "use_ssl": 0, "auto_email_id": "hello@example.com", + "google_analytics_id": "google_analytics_id", + "google_analytics_anonymize_ip": 1, + "google_login": { "client_id": "google_client_id", "client_secret": "google_client_secret" diff --git a/frappe/templates/includes/app_analytics/google_analytics.html b/frappe/templates/includes/app_analytics/google_analytics.html index 65199548b9..7c3b165a8f 100644 --- a/frappe/templates/includes/app_analytics/google_analytics.html +++ b/frappe/templates/includes/app_analytics/google_analytics.html @@ -6,6 +6,9 @@ })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '{{ google_analytics_id }}', 'auto'); + {% if google_analytics_anonymize_ip %} + ga('set', 'anonymizeIp', true); + {% endif %} $(document).on("mousedown", function(event) { if(!frappe && !frappe.get_route) return; diff --git a/frappe/website/doctype/website_settings/website_settings.json b/frappe/website/doctype/website_settings/website_settings.json index 6ad0f2cd01..721bfd2d19 100644 --- a/frappe/website/doctype/website_settings/website_settings.json +++ b/frappe/website/doctype/website_settings/website_settings.json @@ -52,6 +52,7 @@ "indexing_authorization_code", "column_break_17", "google_analytics_id", + "google_analytics_anonymize_ip", "misc_section", "subdomain", "disable_signup", @@ -206,7 +207,6 @@ "label": "Integrations" }, { - "description": "Add Google Analytics ID: eg. UA-89XXX57-1. Please search help on Google Analytics for more information.", "fieldname": "google_analytics_id", "fieldtype": "Data", "label": "Google Analytics ID" @@ -401,6 +401,12 @@ "fieldname": "edit_footer_template_values", "fieldtype": "Button", "label": "Edit Values" + }, + { + "default": "1", + "fieldname": "google_analytics_anonymize_ip", + "fieldtype": "Check", + "label": "Google Analytics Anonymize IP" } ], "icon": "fa fa-cog", @@ -409,7 +415,7 @@ "issingle": 1, "links": [], "max_attachments": 10, - "modified": "2020-08-21 14:02:55.168829", + "modified": "2020-09-28 18:47:18.506700", "modified_by": "Administrator", "module": "Website", "name": "Website Settings", @@ -433,4 +439,4 @@ "sort_field": "modified", "sort_order": "ASC", "track_changes": 1 -} +} \ No newline at end of file diff --git a/frappe/www/desk.py b/frappe/www/desk.py index 6cb7c8a077..c6bce850a5 100644 --- a/frappe/www/desk.py +++ b/frappe/www/desk.py @@ -43,6 +43,7 @@ def get_context(context): "boot": boot if context.get("for_mobile") else boot_json, "csrf_token": csrf_token, "google_analytics_id": frappe.conf.get("google_analytics_id"), + "google_analytics_anonymize_ip": frappe.conf.get("google_analytics_anonymize_ip"), "mixpanel_id": frappe.conf.get("mixpanel_id") }) diff --git a/frappe/www/website_script.js b/frappe/www/website_script.js index e31b6812d5..ce9c28e9d9 100644 --- a/frappe/www/website_script.js +++ b/frappe/www/website_script.js @@ -9,6 +9,9 @@ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '{{ google_analytics_id }}', 'auto'); +{% if google_analytics_anonymize_ip %} +ga('set', 'anonymizeIp', true); +{% endif %} ga('send', 'pageview'); // End Google Analytics {%- endif %} diff --git a/frappe/www/website_script.py b/frappe/www/website_script.py index 9d6ba1065e..0bb5a8a80b 100644 --- a/frappe/www/website_script.py +++ b/frappe/www/website_script.py @@ -18,5 +18,11 @@ def get_context(context): context.javascript += "\n" + js if not frappe.conf.developer_mode: - context["google_analytics_id"] = (frappe.db.get_single_value("Website Settings", "google_analytics_id") - or frappe.conf.get("google_analytics_id")) + context['google_analytics_id'] = get_setting('google_analytics_id') + context['google_analytics_anonymize_ip'] = get_setting('google_analytics_anonymize_ip') + +def get_setting(field_name): + """Return value of field_name frok Website Settings or Site Config.""" + website_settings = frappe.db.get_single_value('Website Settings', field_name) + conf = frappe.conf.get(field_name) + return website_settings or conf