GA track outgoing sponsor clikcs

This commit is contained in:
Bjørn Erik Pedersen 2018-02-19 10:32:03 +01:00
parent aca59ac661
commit cf8a937280
No known key found for this signature in database
GPG Key ID: 330E6E2BD4859D8F
4 changed files with 27 additions and 3 deletions

View File

@ -9,7 +9,7 @@
{{- partial "home-page-sections/features-icons" . -}}
</section>
{{ partial "home-page-sections/sponsors.html" (dict "cx" . ) }}
{{ partial "home-page-sections/sponsors.html" (dict "cx" . "gtag" "home" ) }}
{{- partial "home-page-sections/features-single" . -}}

View File

@ -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;}
});
}
</script>
{{ end }}

View File

@ -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 }}
<section class="{{ $.classes_section | default "bg-primary-color-dark b--dark-gray bb bt ph5 pv4 w-100"}}">
<div class="center mw9"> 
<f3 class="b f3 light-gray">Hugo Sponsors</f3>
<div class="flex-ns flex-wrap center justify-between pt3">
{{ range .banners }}
{{ $banner := . }}
{{if .logo}}
<div class="{{$classes_box}} o-100">
{{with .link -}}
<a href="{{.}}?{{ (querify "utm_source" "homepage" "utm_medium" "banner" "utm_campaign" "hugosponsor") | safeURL }}" class="grow">
{{ $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 }}
<a href="{{ $url }}" onclick="trackOutboundLink({{ printf "'%s', '%s'" $gtagID $url | safeJS }}); return false;" class="grow">
{{ else }}
<a href="{{ $url }}" class="grow">
{{ end }}
{{- end}}
<img src="{{ .logo }}" alt="Logo for {{ .name }}" class="img h3 center" />
{{with .link}}</a>{{end}}

View File

@ -29,7 +29,7 @@
</div>
<div class="w-100 w-50-l"> 
{{ 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") }}
</div>
</div>