-1

I'm working on a smart script that reports all button/link clicks into GA. I'd like to separate redirects from file downloads. The tag used is always a with the href attribute specified.

So let's say I have the following hrefs:

"/"
"aboutus/"
"//www.google.com"
"file1.txt"
"file2.PDF"
"file3.jpeg"

Would the most efficient approach be having an array with all the possible file extensions and then matching that with the href or should it be regEx or something else?

Cream Whipped Airplane
  • 1,305
  • 3
  • 13
  • 31
  • A file-downloads is related to the headers in the response that you get from the server. If you take this for example: http://stackoverflow.com/myfile.pdf you will get a page (404) and your browser will not try to download it. – Dekel Nov 08 '16 at 14:32
  • 1
    As Dekel says, *any* URL can result in a file download. `aboutus/` could result in a file download. `/` could result in a file download. – Frédéric Hamidi Nov 08 '16 at 14:32
  • 1
    With server-side scripting & the right headers, any of these could potentially be a file download -- only way to be sure is to check the headers -- check this out: http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript – JKirchartz Nov 08 '16 at 14:32
  • You cannot know if a URL that look like "a.com/b.pdf" resolves to an actual PDF file. You need to open a connection to that URL (and follow redirections/login/take other actions based on the response) and then see what the content-type header look like. Then again, the server can lie in its headers and say that b.pdf is a PDF file but contain other data, so you would need to verify the contents to be sure. – chelmertz Nov 08 '16 at 14:33
  • 1
    okay, i understand, but this is on a site where I know for sure the file downloads will always have a file extension.. (I'm not trying to write some sort of security script that will verify the links for the users sake..) – Cream Whipped Airplane Nov 08 '16 at 14:35
  • Sorry for not reading the question properly. Does this link help? https://support.google.com/analytics/answer/1012044?hl=en#Download – chelmertz Nov 08 '16 at 14:39
  • It would, but we're putting in custom data attributes for more clear tracking, so I can't just go with the simple tracking inside GTM. Instead I need some sort of legacy fallback for the links that have a file extension, but dont have a custom data attribute set.. – Cream Whipped Airplane Nov 08 '16 at 14:50

1 Answers1

2

As already mentioned in the comment, it is impossible to know only from the URL what the browser will do with the link.

The browser reads the response that it gets from the server, and based on the headers in the response it decide what to do (view the page, redirect, download, etc).

I think it will be much better to check on your server-side the links that you create, and if the link is a downloadable-file - add some class to this link:

<a href="file.pdf" class="downloadable">Click to download</a>

And on your javascript code check for <a> tags that have this downloadable class:

$('a.downloadable').click(function() {
    // Do whatever you need once the link was clicked.
})

Another option (If you really want to) is to send a HEAD request, parse the response-headers, and based on the headers to decide what to do:

$('a').click(function(e) {
    e.preventDefault()
    $.ajax({
        type: "HEAD",
        async: true,
        url: URL,
    }).done(function(message,text,jqXHR){
        // here you can use jqXHR.getResponseHeader('header-name);
    });
});

I really advise you against this option, but if you really must do it it's here :)

Dekel
  • 60,707
  • 10
  • 101
  • 129