From cf8a937280d0ee37d9c12a0c511d11e960b16835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 19 Feb 2018 10:32:03 +0100 Subject: [PATCH] GA track outgoing sponsor clikcs --- themes/gohugoioTheme/layouts/index.html | 2 +- themes/gohugoioTheme/layouts/partials/gtag.html | 16 ++++++++++++++++ .../partials/home-page-sections/sponsors.html | 10 +++++++++- .../layouts/partials/site-footer.html | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/themes/gohugoioTheme/layouts/index.html b/themes/gohugoioTheme/layouts/index.html index efc7ef7e7..2f9364c8c 100644 --- a/themes/gohugoioTheme/layouts/index.html +++ b/themes/gohugoioTheme/layouts/index.html @@ -9,7 +9,7 @@ {{- partial "home-page-sections/features-icons" . -}} - {{ partial "home-page-sections/sponsors.html" (dict "cx" . ) }} + {{ partial "home-page-sections/sponsors.html" (dict "cx" . "gtag" "home" ) }} {{- partial "home-page-sections/features-single" . -}} diff --git a/themes/gohugoioTheme/layouts/partials/gtag.html b/themes/gohugoioTheme/layouts/partials/gtag.html index 3e56e156d..b89f2feb1 100644 --- a/themes/gohugoioTheme/layouts/partials/gtag.html +++ b/themes/gohugoioTheme/layouts/partials/gtag.html @@ -6,5 +6,21 @@ gtag('js', new Date()); gtag('config', '{{ . }}'); + +/** +* Function that tracks a click on an outbound link in Analytics. +* Setting the transport method to 'beacon' lets the hit be sent +* using 'navigator.sendBeacon' in browser that support it. +*/ +var trackOutboundLink = function(id, url) { + console.log("track:", id, url) + gtag('event', 'click', { + 'event_category': 'outbound', + 'event_label': id, + 'transport_type': 'beacon', + 'event_callback': function(){document.location = url;} + }); +} + {{ end }} \ No newline at end of file diff --git a/themes/gohugoioTheme/layouts/partials/home-page-sections/sponsors.html b/themes/gohugoioTheme/layouts/partials/home-page-sections/sponsors.html index 527f4dff1..95f7ecfdb 100644 --- a/themes/gohugoioTheme/layouts/partials/home-page-sections/sponsors.html +++ b/themes/gohugoioTheme/layouts/partials/home-page-sections/sponsors.html @@ -1,14 +1,22 @@ {{$classes_box := "ba b--light-gray bg-light-gray br3 flex flex-column flex-wrap items-center justify-center ph3 pv4 mb4 w-100 w-30-l "}} +{{$gtag := .gtag | default "unknown" }} {{ with .cx.Site.Data.sponsors }}
  Hugo Sponsors
{{ range .banners }} + {{ $banner := . }} {{if .logo}}
{{with .link -}} - + {{ $url := printf "%s?%s" . (querify "utm_source" "homepage" "utm_medium" "banner" "utm_campaign" "hugosponsor") | safeURL }} + {{ if eq (getenv "HUGO_ENV") "production" | or (eq $.cx.Site.Params.env "production") }} + {{ $gtagID := printf "Sponsor %s %s" $banner.name $gtag | title }} + + {{ else }} + + {{ end }} {{- end}} Logo for {{ .name }} {{with .link}}{{end}} diff --git a/themes/gohugoioTheme/layouts/partials/site-footer.html b/themes/gohugoioTheme/layouts/partials/site-footer.html index ef32025eb..771ec348f 100755 --- a/themes/gohugoioTheme/layouts/partials/site-footer.html +++ b/themes/gohugoioTheme/layouts/partials/site-footer.html @@ -29,7 +29,7 @@
  - {{ partial "home-page-sections/sponsors.html" (dict "cx" . "classes_section" "pb3 w-100" "classes_copy" "f7 w-90-ns") }} + {{ partial "home-page-sections/sponsors.html" (dict "cx" . "gtag" "footer" "classes_section" "pb3 w-100" "classes_copy" "f7 w-90-ns") }}