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.
Copyright Notice:Content Author:「Abdul Rafay Shaikh」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37328250/google-analytics-track-events-in-iframe-but-not-the-pageview