Javascript Redirect with Google Analytics

后端 未结 9 677
暗喜
暗喜 2020-11-28 22:30

I need help figuring out how to successfully redirect while including Analytics code.

  • I have a subdomain setup http://buuf.fractalsystems.org
  • The subd
相关标签:
9条回答
  • 2020-11-28 22:58

    Meta refresh is generally discouraged because of usability concerns (especially with a short, or no delay), but as a fallback for clients with no javascript I think it's perfectly valid in this case.

    If you combine the synchronous ga.js call with a meta refresh you get the best of both worlds: an almost instant, tracked redirect if JS is enabled; a delayed but still effective redirect if not (and a hard link in the body just in case all else fails).

    <html>
    <head>
        <!--For JS disabled: decent delay, both for usability and to allow plenty of time for JS to work if enabled -->
        <meta http-equiv="refresh" content="5;https://market.android.com/developer?pub=Fractal%20Systems"/>
        <script>
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-1234567-8']);
            _gaq.push(['_trackPageview']);
        </script>
        <!--Synchronous call to ensure tracking fires before JS redirect-->
        <script type="text/javascript" src="//www.google-analytics.com/ga.js"></script>
        <script>
            /* if JS is enabled this will normally fire well before the meta refresh delay ends: an almost instantaneous redirect */
            window.location="https://market.android.com/developer?pub=Fractal%20Systems";
        </script>
    </head>
    <body>
        <!-- Include a hard link in case both js and the meta refresh fail -->
        <p>Redirecting you to <a href="https://market.android.com/developer?pub=Fractal%20Systems">https://market.android.com/developer?pub=Fractal%20Systems</a></p>
    </body></html>
    
    0 讨论(0)
  • 2020-11-28 23:00

    I'd suggest changing your Google Analytics code to be synchronous instead of asychronous by changing it to this:

    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-1234567-8']);
      _gaq.push(['_trackPageview']);
    </script>
    <script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
    

    This should guarantee that it runs before your redirect code kicks in and it should be out of the way of your redirect script so there is no interference. As you have it now, you're playing a guessing game for how long the GA script will take to load and that it will load and do it's job in under 3 seconds. That may usually be the case, but there is no reason to load it asynchronously like you are and have to play that game. Load it synchronously and it will do it's job before your javascript redirect fires.

    It might even be possible to just put the redirect directly after the GA code like this and minimize the time that your placeholder page is displayed:

    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-1234567-8']);
      _gaq.push(['_trackPageview']);
    </script>
    <script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
    <script type="text/javascript">
      window.location = "https://market.android.com/developer?pub=Fractal%20Systems";
    </script>
    
    0 讨论(0)
  • 2020-11-28 23:09

    There's a good answer here: https://www.domsammut.com/code/setting-up-hitcallback-using-google-universal-analytics

    In short, you'd do it using an event, and you use the hitCallback function.

    0 讨论(0)
  • 2020-11-28 23:11

    If you're using the new GA code you can simply replace this line ga('send', 'pageview'); with this code:

    ga('send', 'pageview', {
      'hitCallback': function() {
          window.location = "http://www.your-site.com";
      }
    });
    

    Example in full:

      (function(i,s,o,g,r,a,m){
        i['GoogleAnalyticsObject']=r;
        i[r]=i[r]||function() {
            (i[r].q=i[r].q||[]).push(arguments)
        },i[r].l=1*new Date();
        a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];
        a.async=1;
        a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
    ga('create', 'UA-xxxxxxx-2', 'auto');
    
    ga('send', 'pageview', {
      'hitCallback': function() {
          window.location = "http://www.your-site.com";
      }
    });
    
    0 讨论(0)
  • 2020-11-28 23:12

    I think maybe just use jquery ready method so it won't start the timer till the page is fully loaded...

    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {       
        setTimeout(function() {
            //alert("redirecting!");
            window.location = '<?= $url ?>';
        }, 3000);
    });
    </script>
    
    0 讨论(0)
  • 2020-11-28 23:13

    This approach does not require a delay. First execute google analytics code synchronously and then redirect.

    <html>
    <head>
    <script src="//www.google-analytics.com/analytics.js"></script>
      <script>
        var tracker = ga.create('UA-xxxxxxxx-x', 'auto');
        tracker.send('pageview');
        window.location='http://your-url.com';
      </script>
    </head>
    <body>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题