javascript - How to make a redirection that fired a script before redirecting?

I have a software that I must host in an external server in a niche market (kind of extensions for browsers). I want to

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:

window.location.replace("");

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?

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:

window.location.replace("http://stackoverflow");

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?

Share Improve this question edited Jan 23, 2020 at 12:40 Ooker asked Nov 20, 2019 at 17:24 OokerOoker 3324 silver badges23 bronze badges 8
  • 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 Commented 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 Commented 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 Commented 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 Commented 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 Commented Jan 21, 2020 at 14:56
 |  Show 3 more comments

1 Answer 1

Reset to default 0

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

   .then(
      function(response){
         window.location.href = 'http://www.google'; //for example
      },function(error){
        console.log('Error: ' + error.statusText);
       }
    );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744809705a4594991.html

相关推荐

  • javascript - How to make a redirection that fired a script before redirecting?

    I have a software that I must host in an external server in a niche market (kind of extensions for browsers). I want to

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信