mirror of
https://github.com/discourse/discourse.git
synced 2025-04-01 14:08:09 +08:00
FEATURE: Google Tag Manager Universal Analytics support
This commit is contained in:
parent
f8a12d4940
commit
ba637e40b6
app
assets/javascripts/discourse/initializers
helpers
views
config
@ -50,5 +50,18 @@ export default {
|
|||||||
window.ga('send', 'pageview', {page: url, title: title});
|
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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -15,8 +15,8 @@ module ApplicationHelper
|
|||||||
include ConfigurableUrls
|
include ConfigurableUrls
|
||||||
include GlobalPath
|
include GlobalPath
|
||||||
|
|
||||||
def ga_universal_json
|
def google_universal_analytics_json(ua_domain_name)
|
||||||
cookie_domain = SiteSetting.ga_universal_domain_name.gsub(/^http(s)?:\/\//, '')
|
cookie_domain = ua_domain_name.gsub(/^http(s)?:\/\//, '')
|
||||||
result = {cookieDomain: cookie_domain}
|
result = {cookieDomain: cookie_domain}
|
||||||
if current_user.present?
|
if current_user.present?
|
||||||
result[:userId] = current_user.id
|
result[:userId] = current_user.id
|
||||||
@ -24,6 +24,14 @@ module ApplicationHelper
|
|||||||
result.to_json.html_safe
|
result.to_json.html_safe
|
||||||
end
|
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
|
def shared_session_key
|
||||||
if SiteSetting.long_polling_base_url != '/'.freeze && current_user
|
if SiteSetting.long_polling_base_url != '/'.freeze && current_user
|
||||||
sk = "shared_session_key"
|
sk = "shared_session_key"
|
||||||
|
@ -10,4 +10,10 @@ module CommonHelper
|
|||||||
render partial: "common/google_analytics"
|
render partial: "common/google_analytics"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
11
app/views/common/_google_tag_manager.html.erb
Normal file
11
app/views/common/_google_tag_manager.html.erb
Normal 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>
|
@ -44,6 +44,8 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="<%= body_classes %>">
|
<body class="<%= body_classes %>">
|
||||||
|
<%= render_google_tag_manager_code %>
|
||||||
|
|
||||||
<div class='debug-eyeline'></div>
|
<div class='debug-eyeline'></div>
|
||||||
|
|
||||||
<noscript data-path="<%= request.env['PATH_INFO'] %>">
|
<noscript data-path="<%= request.env['PATH_INFO'] %>">
|
||||||
|
@ -914,6 +914,8 @@ en:
|
|||||||
ga_domain_name: "Google analytics (ga.js) domain name, eg: mysite.com; see http://google.com/analytics"
|
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_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"
|
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_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"
|
enable_noscript_support: "Enable standard webcrawler search engine support via the noscript tag"
|
||||||
allow_moderators_to_create_categories: "Allow moderators to create new categories"
|
allow_moderators_to_create_categories: "Allow moderators to create new categories"
|
||||||
|
@ -97,6 +97,13 @@ basic:
|
|||||||
ga_domain_name:
|
ga_domain_name:
|
||||||
client: true
|
client: true
|
||||||
default: ''
|
default: ''
|
||||||
|
gtm_container_id:
|
||||||
|
client: true
|
||||||
|
default: ''
|
||||||
|
regex: "^GTM-"
|
||||||
|
gtm_ua_domain_name:
|
||||||
|
client: true
|
||||||
|
default: 'auto'
|
||||||
top_menu:
|
top_menu:
|
||||||
client: true
|
client: true
|
||||||
refresh: true
|
refresh: true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user