-2

I have found this code on github to automatically track outgoing URLs with gtag.js.

<script>
    var trackOutboundLink = function(url) {
      gtag('event', 'click', {
        'event_category': 'outbound',
        'event_label': url,
        'transport_type': 'beacon',
        'event_callback': function(){document.location = href;}
      });
    }
    </script>

    <script>
    jQuery(document).ready(function($) {
       $('a[href^="http"]:not([href*="//' + location.host + '"])').on('click', 
            function(e) {
                trackOutboundLink($(this).attr("href")); 
                return true;
            });
    });
    </script>

But I get the following error:

Uncaught ReferenceError: href is not defined

When I put 'event_callback': function(){document.location = url;} it would work, but then the link is opened twice in a new window and in the same tab.

I am a dummy with this stuff... could you put me in the right direction?

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • `.location = url;`, `return false` - but this will break the navigation when `gtag` doesn't execute the `event_callback` (for whatever reason) – Andreas Dec 20 '19 at 10:13
  • Where is your `href` variable defined and what you expect it to be/happen? – Jorge Fuentes González Dec 20 '19 at 10:15
  • 2
    `document.location = href` ...where is `href` supposed to come from here? As far as I can see, it isn't defined - which is exactly what the error message is telling you. You have never created a variable with that name or given it a value. Please explain what this line of code was intended to do, and where you want to get the value of `href` from. – ADyson Dec 20 '19 at 10:15
  • Unfortunately I cannot answer to your questions... as I said, I just found the code on github. – Edith Mayerhofer Dec 20 '19 at 11:15

2 Answers2

1

OK - this is working as supposed now:

<script>
var trackOutboundLink = function(url) {
  gtag('event', 'click', {
    'event_category': 'outbound',
    'event_label': url,
    'transport_type': 'beacon',
    'event_callback': function(){window.open(url);}
  });
}
</script>

<script>
jQuery(document).ready(function($) {
   $('a[href^="http"]:not([href*="//' + location.host + '"])').on('click', 
        function(e) {
        e.preventDefault();
            trackOutboundLink($(this).attr("href")); 
            return true;
        });
});
</script>
-1

Try this may be it would help

<script>
    var trackOutboundLink = function(url) {
      gtag('event', 'click', {
        'event_category': 'outbound',
        'event_label': url,
        'transport_type': 'beacon',
        'event_callback': function(){window.open(url);}
      });
    }
    </script>

    <script>
    jQuery(document).ready(function($) {
       $('a[href^="http"]:not([href*="//' + location.host + '"])').on('click', 
            function(e) {
            e.preventDefault();
                trackOutboundLink($(this).attr("href")); 
                return true;
            });
    });
    </script>
shanu
  • 76
  • 1
  • 1
  • 11