I have a software that I must host in an external server in a niche market (kind of extensions for browsers). I want to know how many people click on the link when I share it on social media.

My idea is to redirect a post in my site to it, but before the redirection happens the Google Analytics script should be fired, so that I get the report. The Redirection plugin can be used for redirection, but it doesn't support that yet. One go-around idea is to create a post and add this:


but it may increase the loading time for loading the header. Plus that it doesn't guarantee that the script will be fired, as it can be included at the footer.

Is there a way to make sure that the script is fired before redirection?

  • In order to answer this question properly, a lot more information would need to be known about the real "why" you're trying to do this. It is never a good idea to use JS to perform a redirect, and the situation you described as having a URL on your site only exist to redirect to an external site has some potential ethical implications. I can think of only a few legitimate scenarios where you would want to redirect the traffic to an external site and capture the traffic in your analytics. The first question would be, do you control the external site?
    – KFish
    Jan 18, 2020 at 1:25
  • Technically yes. The content of the specific page I'm trying to redirect at is mine. Would that be OK? And even if it's not, then why would it be an ethical issue?
    – Ooker
    Jan 19, 2020 at 8:43
  • If you control the external site then remove your JS redirect, perform the redirect properly on the server side as a 301 or 302, and add your necessary analytics code to the target page to track the referrer. Otherwise, it isn't your content or really your traffic, and your URL is never going to be indexed for that content anyway.
    – KFish
    Jan 19, 2020 at 19:30
  • Say I post a clip on YouTube. Do you mean that even if I track the redirection, I will never get a complete report on the traffic on that clip? Why wouldn't that clip be my content? I see that that this is just a simple outbound-tracking case?
    – Ooker
    Jan 20, 2020 at 15:39
  • 1
    See, this is why I said you need to start with the first question "why am I doing this", that's the first step to finding the right solution. I don't know why you would want to create a post/url, just to redirect it to your youtube video. Technically, yes it's your video, it's also technically YouTube's content. Google is not ever going to index your url over YouTube's, yours only contains a small bit of JS, nothing else. They might even penalize your site for doing that. Why not just make it an actual page and embed the video?
    – KFish
    Jan 21, 2020 at 14:56

1 Answer 1


Place both scripts in the footer. In order, put Google scripts first, wrap in a promise, then add:

         window.location.href = 'http://www.google.com'; //for example
        console.log('Error: ' + error.statusText);
  • but then this would fire all things in the header and body? Doesn't this not reduce the loading time?
    – Ooker
    Nov 21, 2019 at 4:09
  • Everything in the header and body would be fired, but that's also necessary if you want real analytics from Google. If you're simply looking for the hit count, then you could fire it in the header. Nov 22, 2019 at 14:26
  • yes, for the time being I just need hit count. What do you mean by "real analytics"?
    – Ooker
    Nov 23, 2019 at 12:20
  • If you fire in the header, you'll miss out on any session information or demographic info. But that's not a big deal given your application. Nov 24, 2019 at 20:38
  • 1
    A rudimentary amount of information will present, but there is an amount of time and interaction necessary for all metrics to be displayed. Nov 25, 2019 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.