From 65bfa574cef2ef1d903494ef53870add3f7da755 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 7 Apr 2015 13:09:49 -0400 Subject: [PATCH] Add `userId` to GA tracking --- app/helpers/application_helper.rb | 9 +++++++++ app/views/common/_google_universal_analytics.html.erb | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3edce092e6..f49d845b2c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -15,6 +15,15 @@ module ApplicationHelper include ConfigurableUrls include GlobalPath + def ga_universal_json + cookie_domain = SiteSetting.ga_universal_domain_name.gsub(/^http(s)?:\/\//, '') + result = {cookieDomain: cookie_domain} + if current_user.present? + result[:userId] = current_user.id + end + result.to_json.html_safe + end + def shared_session_key if SiteSetting.long_polling_base_url != '/'.freeze && current_user sk = "shared_session_key" diff --git a/app/views/common/_google_universal_analytics.html.erb b/app/views/common/_google_universal_analytics.html.erb index 8514da2f0a..39f7baf994 100644 --- a/app/views/common/_google_universal_analytics.html.erb +++ b/app/views/common/_google_universal_analytics.html.erb @@ -4,7 +4,7 @@ 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', '<%= SiteSetting.ga_universal_tracking_code %>', '<%= SiteSetting.ga_universal_domain_name.gsub(/^http(s)?:\/\//, '') %>'); + ga('create', '<%= SiteSetting.ga_universal_tracking_code %>', <%= ga_universal_json %>); ga('send', 'pageview');