pankaj sandhu
pankaj sandhu

Reputation: 13

auto click not working in javascript

var myLink2 = document.getElementsByClassName('top-widget-inner');
for (var i = 0; i < myLink2.length; i++) {
    var link = document.getElementsByClassName('top-widget-inner')[i].getElementsByTagName('a')[0];
    link.click();
}

html:

<div class="top-widget"> <div class="top-widget-inner static4"> <a target="_blank" onclick="trackOutboundLink('ramptonbaseley.com/'); return false;" href="ramptonbaseley.com/">; <img src="nappyvalleynet.com/wp-content/uploads//2014/05/…; alt=""> </a> </div> </div>

When I refresh or reload my page, link.click() function is working fine. But when I check the Click events(by js) on google analytics, it's show me nothing. How to improve my code ? thanking you.

Upvotes: 1

Views: 228

Answers (2)

Mayank Kushwaha
Mayank Kushwaha

Reputation: 344

   <script type="text/javascript">
    (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','https://www.google-analytics.com/analytics.js','ga');
            ga('create', 'put_your_trackID_here', 'auto');
            ga('send', 'pageview');
    </script>

You can put this code in your file, in header or footer. It'll show your events on this page https://analytics.google.com under real-time event tab.

It'll work.

Upvotes: 0

Andre Nel
Andre Nel

Reputation: 442

Page at https://developers.google.com/analytics/devguides/collection/analyticsjs/events

Outbound link and form tracking

When a user clicks a link that points to another page on your site, that page typically sends a pageview hit as the user arrives. Because there's a series of pageviews, Google Analytics can figure out on the back end where the user navigated to (and from). But if a user clicks a link or submits a form to an external domain, that action is not captured unless you specifically tell Google Analytics what happened.

Outbound link and form tracking can be accomplished by sending events and specifying the destination URL in one of the event fields. The following event handler function can be used to send outbound link click events to Google Analytics:

function handleOutboundLinkClicks(event) {
  ga('send', 'event', {
    eventCategory: 'Outbound Link',
    eventAction: 'click',
    eventLabel: event.target.href
 });
}

Tracking outbound links and forms can be tricky because most browsers will stop executing JavaScript on the current page once a new page starts to load. One solution to this problem is to set the transport field to beacon:

function handleOutboundLinkClicks(event) {
  ga('send', 'event', {
    eventCategory: 'Outbound Link',
    eventAction: 'click',
    eventLabel: event.target.href,
    transport: 'beacon'
  });
}

For browsers the don't support the beacon transport method, you have to postpone navigating to the next page until the event has finished sending. The Knowing when a hit has been sent section of the guide on Sending data to Google Analytics explains how to do this in detail.

Upvotes: 1

Related Questions