From ba637e40b6ed4e3175a86a27d27e01f0bd08a3c1 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 14 Jul 2016 13:52:37 -0400 Subject: [PATCH] FEATURE: Google Tag Manager Universal Analytics support --- .../discourse/initializers/page-tracking.js.es6 | 13 +++++++++++++ app/helpers/application_helper.rb | 12 ++++++++++-- app/helpers/common_helper.rb | 6 ++++++ app/views/common/_google_tag_manager.html.erb | 11 +++++++++++ app/views/layouts/application.html.erb | 2 ++ config/locales/server.en.yml | 2 ++ config/site_settings.yml | 7 +++++++ 7 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 app/views/common/_google_tag_manager.html.erb diff --git a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 index 7f680580ea3..860ddaf0f80 100644 --- a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 +++ b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 @@ -50,5 +50,18 @@ export default { window.ga('send', 'pageview', {page: url, title: title}); }); } + + // And Google Tag Manager too + if (typeof window.dataLayer !== 'undefined') { + onPageChange((url, title) => { + window.dataLayer.push({ + 'event': 'virtualPageView', + 'page': { + 'title': title, + 'url': url + } + }); + }); + } } }; diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 06d782214fa..c8321c795cc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -15,8 +15,8 @@ module ApplicationHelper include ConfigurableUrls include GlobalPath - def ga_universal_json - cookie_domain = SiteSetting.ga_universal_domain_name.gsub(/^http(s)?:\/\//, '') + def google_universal_analytics_json(ua_domain_name) + cookie_domain = ua_domain_name.gsub(/^http(s)?:\/\//, '') result = {cookieDomain: cookie_domain} if current_user.present? result[:userId] = current_user.id @@ -24,6 +24,14 @@ module ApplicationHelper result.to_json.html_safe end + def ga_universal_json + google_universal_analytics_json(SiteSetting.ga_universal_domain_name) + end + + def google_tag_manager_json + google_universal_analytics_json(SiteSetting.gtm_ua_domain_name) + end + def shared_session_key if SiteSetting.long_polling_base_url != '/'.freeze && current_user sk = "shared_session_key" diff --git a/app/helpers/common_helper.rb b/app/helpers/common_helper.rb index 10992b1ae6b..e6107369038 100644 --- a/app/helpers/common_helper.rb +++ b/app/helpers/common_helper.rb @@ -10,4 +10,10 @@ module CommonHelper render partial: "common/google_analytics" end end + + def render_google_tag_manager_code + if Rails.env.production? && SiteSetting.gtm_container_id.present? + render partial: "common/google_tag_manager" + end + end end diff --git a/app/views/common/_google_tag_manager.html.erb b/app/views/common/_google_tag_manager.html.erb new file mode 100644 index 00000000000..dca75352a06 --- /dev/null +++ b/app/views/common/_google_tag_manager.html.erb @@ -0,0 +1,11 @@ + + + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3b8b4ccc61f..af5e7df438d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -44,6 +44,8 @@ + <%= render_google_tag_manager_code %> +