google_analytics.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. {{ $ga := site.Params.marketing.analytics.google_analytics | default site.GoogleAnalytics | default "" }}
  2. {{ if hugo.IsProduction | and $ga }}
  3. {{ $gtag_config := cond site.Params.features.privacy_pack.enable "{ 'anonymize_ip': true }" "{}" }}
  4. <script async src="https://www.googletagmanager.com/gtag/js?id={{$ga}}"></script>
  5. <script>
  6. window.dataLayer = window.dataLayer || [];
  7. function gtag() {
  8. dataLayer.push(arguments);
  9. }
  10. function trackOutboundLink(url, target) {
  11. gtag('event', 'click', {
  12. 'event_category': 'outbound',
  13. 'event_label': url,
  14. 'transport_type': 'beacon',
  15. 'event_callback': function () {
  16. if (target !== '_blank') {
  17. document.location = url;
  18. }
  19. }
  20. });
  21. console.debug("Outbound link clicked: " + url);
  22. }
  23. function onClickCallback(event) {
  24. if ((event.target.tagName !== 'A') || (event.target.host === window.location.host)) {
  25. return;
  26. }
  27. trackOutboundLink(event.target, event.target.getAttribute('target')); // Send GA event.
  28. }
  29. gtag('js', new Date());
  30. gtag('config', '{{$ga}}', {{$gtag_config|safeJS}});
  31. gtag('set', {'cookie_flags': 'SameSite=None;Secure'});
  32. // Outbound link tracking.
  33. document.addEventListener('click', onClickCallback, false);
  34. </script>
  35. {{ end }}