Today I set up a custom shortlink: pointing to a Google Form (Click it if you speak french and are interested in online fashiown game, we’re doing a survey !)

The website is hosted with GitHub Pages, so I cannot go with an HTTP redirect. And I want to track the origin of the shortlink users through my Piwik instance.

[Of course] I did not find anybody who had the same use-case on Google. But I found two interesting articles: Here someone explains how to replicate the behavior of the jekyll-redirect-from plugin (without tracking) and here there are a few interesting ways to do a javascript redirect while tracking it with Google Analytics. I find this one particularly interesting. It involves turning the async tracking code into a sync one, and put the redirect after the tracking code. This prevents the redirect from happening before the tracking, and we don’t need hard-coded waiting times.

You can have a look at the actual file here, but below is a more generic version you are welcome to edit to fit your needs:

And to use it:

Note: The SO answer I based my code on states that you should not use page.redirect_to as the parameter name, as it will conflict with the jekyll-redirect-to plugin, which I believe is enabled on GitHub Pages (didn’t test myself).

So far it seems this method works !