1

I am working on this javascript code and when is goes in the head and I refresh it goes to a 404 page.

Please can someone help.

<script type="text/javascript">
function recordOutboundLink(link, category, action) {
    try {
        var myTracker = _gat._getTrackerByName();
        _gaq.push(['myTracker._trackEvent', category, action]);
        setTimeout('document.location = "' + link.href + '"', 100)
    } catch (err) { }
}
$(document).ready(function () { $('#myid').click(recordOutboundLink(this, 'regular   xxxxx', 'xxxx.example.com')); });
 </script>
0

2 Answers 2

3

You're trying to register the result of recordOutboundLink() as a click handler, causing the function to run first, evaluating window.href as the page to redirect to. The value of window.href is typically undefined, so the browser will try to redirect to http://undefined or something similar.

Instead, you should only execute the function when something is clicked, like so:

$(document).ready(function () { 
    $('#myid').click(function() {
        recordOutboundLink(this, 'regular   xxxxx', 'http://xxxx.example.com');
        return false;
    });

I believe the Google docs mention something like this:

<a href="bla bla" onclick="recordOutboundLink(this, 'regular crap', 'http://www.example.com'); return false;">tada click me</a>

Edit

Your locations should always be absolute, i.e. start with http://, https:// or simply //.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jack, I can't edit the HTML on this specific issue (cms plugin), so I only have access to the JS in the head. Thanks
@jelly46 Is there a reason why you changed the accepted answer? Didn't it work?
1

You need to pass the complete url to the method, ie, with the http:// part

so either use:

.click(recordOutboundLink(this, 'regular   xxxxx', 'http://xxxx.example.com'))

or

.click(recordOutboundLink(this, 'regular   xxxxx', '//xxxx.example.com'))

1 Comment

Hello thanks for that, I did that but i am still getting the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.