1
votes

I am trying to track events in iframe through google analytics its not cross domain but I need to call the content in an iframe and track events. I used this object to send an event to google analytics.

var tracking = {
 event_tracker : function(event,ad_id) {
    var eventName = "";
    switch (event){
        case "1":
            eventName = "Click";
            break;
        case "2":
            eventName = "Impression";
            break;
    }
    console.log(ad_id);
    ga('send', 'event', ad_id, eventName);
}

and this is the simple event I want to fetch

<a href="#" onclick="tracking.event_tracker('1','event1')">
   <img onload="tracking.event_tracker('2','event2');" src="dummyimage.jpg" alt="ads">
 </a>

one onClick and one of the image Impression I am putting this code in my iframe just to track event not the Page view as in the documentation

(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-xxx-1', 'dummy.com');

as you can see I havent added

ga('send', 'pageview');

that sends the pageview event but Google analytics is still showing pageviews.

Question: Is there a way to track only events and not pageviews in google analytics.

1
I'm having the same problem. Did you get the answer? - Bruno Almeida
no @BrunoAlmeida I didnt get the answer to this question. - Abdul Rafay Shaikh

1 Answers

0
votes

You need to set the optional parameter nonInteraction to true as described on Google Analytics Documentation.

I was following the Cross-domain Iframe configuration and had the same issue.

Adding the nonInteraction parameter like the code bellow solved my case.

ga('create', 'UA-XXXXXXXX-X', {
  clientId: event.data
});

ga('send', 'event', 'Forms', 'Viewed', 'My custom form', {
  nonInteraction: true
});

I hope this works for you too.