feat: use gtag for G-* IDs

This commit is contained in:
Sergey Motornyuk 2022-08-23 17:11:41 +03:00
parent 12e08b8cd8
commit 0257299b5a
5 changed files with 52 additions and 14 deletions

View File

@ -6,6 +6,7 @@ def get_helpers():
return {
"googleanalytics_header": googleanalytics_header,
"googleanalytics_resource_prefix": googleanalytics_resource_prefix,
"googleanalytics_tracking_mode": googleanalytics_tracking_mode,
}
@ -37,3 +38,7 @@ def googleanalytics_header():
return tk.render_snippet(
"googleanalytics/snippets/googleanalytics_header.html", data
)
def googleanalytics_tracking_mode():
return utils.config_tracking_mode()

View File

@ -0,0 +1,14 @@
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
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', '{{googleanalytics_id}}', '{{googleanalytics_domain}}', {{googleanalytics_fields|safe}});
{% if googleanalytics_linked_domains %}
ga('require', 'linker');
ga('linker:autoLink', [{% for domain in googleanalytics_linked_domains %}'{{domain}}'{% if not loop.last %},{% endif %}{% endfor %}] );
{% endif %}
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>

View File

@ -0,0 +1,16 @@
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{googleanalytics_id}}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('set', 'linker', );
gtag('js', new Date());
gtag('config', '{{googleanalytics_id}}', {
anonymize_ip: true,
linker: {
domains: {{ googleanalytics_linked_domains|tojson }}
}
});
</script>

View File

@ -1,14 +1 @@
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
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', '{{googleanalytics_id}}', '{{googleanalytics_domain}}', {{googleanalytics_fields|safe}});
{% if googleanalytics_linked_domains %}
ga('require', 'linker');
ga('linker:autoLink', [{% for domain in googleanalytics_linked_domains %}'{{domain}}'{% if not loop.last %},{% endif %}{% endfor %}] );
{% endif %}
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
{% include 'googleanalytics/snippets/_' ~ h.googleanalytics_tracking_mode() ~ '.html' %}

View File

@ -10,6 +10,22 @@ def config_id():
return tk.config["googleanalytics.id"]
def config_tracking_mode():
type_ = tk.config.get("googleanalytics.tracking_mode")
if type_:
return type_
id_ = config_id()
if id_.startswith("UA-"):
return "ga"
if id_.startswith("G-"):
return "gtag"
return "ga"
def config_account():
return tk.config.get("googleanalytics.account")