FEATURE: Google Tag Manager Universal Analytics support

This commit is contained in:
Neil Lalonde 2016-07-14 13:52:37 -04:00
parent f8a12d4940
commit ba637e40b6
7 changed files with 51 additions and 2 deletions

View File

@ -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
}
});
});
}
}
};

View File

@ -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"

View File

@ -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

View File

@ -0,0 +1,11 @@
<script>
dataLayer = [<%= google_tag_manager_json %>];
</script>
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=<%= SiteSetting.gtm_container_id %>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<%= SiteSetting.gtm_container_id %>');</script>

View File

@ -44,6 +44,8 @@
</head>
<body class="<%= body_classes %>">
<%= render_google_tag_manager_code %>
<div class='debug-eyeline'></div>
<noscript data-path="<%= request.env['PATH_INFO'] %>">

View File

@ -914,6 +914,8 @@ en:
ga_domain_name: "Google analytics (ga.js) domain name, eg: mysite.com; see http://google.com/analytics"
ga_universal_tracking_code: "Google Universal Analytics (analytics.js) tracking code code, eg: UA-12345678-9; see http://google.com/analytics"
ga_universal_domain_name: "Google Universal Analytics (analytics.js) domain name, eg: mysite.com; see http://google.com/analytics"
gtm_container_id: "Google Tag Manager container id. eg: GTM-ABCDEF"
gtm_ua_domain_name: "Universal Analytics via Google Tag manager domain name. 'auto' is recommended."
enable_escaped_fragments: "Fall back to Google's Ajax-Crawling API if no webcrawler is detected. See https://developers.google.com/webmasters/ajax-crawling/docs/learn-more"
enable_noscript_support: "Enable standard webcrawler search engine support via the noscript tag"
allow_moderators_to_create_categories: "Allow moderators to create new categories"

View File

@ -97,6 +97,13 @@ basic:
ga_domain_name:
client: true
default: ''
gtm_container_id:
client: true
default: ''
regex: "^GTM-"
gtm_ua_domain_name:
client: true
default: 'auto'
top_menu:
client: true
refresh: true