Track outbound links with Google Analytics

Hugo currently supports Google Analytics out of the box, but without tracking of outbound links.

See Track outbound links - Find out when users click a link to leave your site

Implementation would involve:

  1. Add the trackOutboundLink script to the google analytics internal templates
  2. Add the onclick attribute to all external links, either using some javascript to iterate through all the <a> elements on the page, or by adding a new feature to BlackFriday

Questions I’d like to discuss before I open an Issue or PR:

  1. Should outbound link tracking be implemented as a feature in Hugo? (or let people implement it in themes)

  2. If implemented as a feature in Hugo, should it be enabled by default? (iff GA is enabled)

  3. What’s the best way to add the onclick attribute to external links? (javascript - quick and easy to implement but will impact page loading; or BlackFriday - much more elegant, but requires changes to BlackFriday)

Please discuss; thanks in advance.

We already do some Blackfriday and Mmark link manipulation, so this doesn’t need a change in those, but the question is: Is it worth it?

I don’t think it should since I estimate that only a small subset of people have a need to track outbound links.

Isn’t it much easier to add the Google Analytics snippet yourself if you want to make customisations? I never use the Hugo internal templates because adding the tracking code myself is super quick and allows for lots of customisations.

I also think it will be good if Hugo’s internal templates for Google Analytics cover just the basics. Otherwise we’d have feature requests for setting a custom site speed sample, implementing the anonymizeIp feature, or tracking the custom bounce rate.

My point: there’s a lot we can do with Google Analytics, and as site developers we probably do, but this is not the stuff we should expect from Hugo core. But that are just my 2 cents. :slightly_smiling:

1 Like

I agree that Hugo’s implementation of GA should be the simple plain vanilla. It’s really not hard to do on one’s own. As something of an aside, this looks like a neat implementation: https://github.com/googleanalytics/autotrack

1 Like

Thanks @budparr and @jura.

Thanks for this link! now implemented in my site and :crossed_fingers: I’ll start seeing events again!

1 Like

I have added a new update GA partial to the gohugo.io sites

Also see:

I had a hard time digging into the GA docs for the dimension syntax, but it is really useful to create segments.