GTM&GA: Reusable Download and Outgoing Link Event

Google Tag Manager has a great feature – setup export and import. That is why it is worth reusing the setup as much as possible – i. e. independent of the CMS used or the specific website. So, how do you set up a universal Download link click event?

Downloaded files

Some time ago I used to use a trigger condition based on the folder where all the files are supposed to be (e.g. /files/). So if anyone clicked on a link containing “/files/” in the path, it matched the condition and was recorded as a Download event. But when using this approach the setup is dependent on the specific CMS or website.
We can base our trigger condition on file extensions and we will a get (nearly) universal rule.
The regular expression below covers the 132 most common file extensions.


You can use this regular expression in the GTM trigger setup (see the image below).


By this I mean clicks on links to another website (i.e. another domain).
In the past I have used the condition:
{{Click URL}} does not match regular expression ^(http|https|ftp|ftps):\/\/(www\.mywebsite\.com) i. e. a regular expression describing the domain of my website.
Now I have a universal solution with no need to explicitly specify my domain.
I use a short javascript code as the GTM Variable (called isExternalLink). Every time someone clicks on the link, the variable is evaluated and returns true or false to indicate if the link is pointing to a different domain (i. e. outgoing link).

    var hostname = window.location.hostname;
    var elementUrl = "{{Click URL}}";

    var getLocation = function(href) {
        var l = document.createElement("a");
        l.href = href;
        return l;

    if (hostname == getLocation(elementUrl).hostname) {
        return "false";
    } else {
        return "true";



Then in Fire on condition we test isExternalLink = true.


Leave a Reply

Your email address will not be published. Required fields are marked *