I am using the following code in a WordPress sidebar widget, to load random advertisement which is working perfectly. This code also sends impressions to google Analytics. Now the issue is that this code sends impressions to all advertisements. not only the advertisement loaded randomly. I have pasted the code in the below url.
<script>
var value1 = 'top_1st';
var value2 = 'top_2nd';
var value3 = 'top_3rd';
var chosenValue = Math.random() < 0.5 ? value1 : value2 : value3;
var chosenDiv = document.getElementById(chosenValue);
chosenDiv.style.display = "block";
</script>
<div id="inline" class="various2">
<div style="display: none;" id="top_1st">
<a href="http://scicom.mv" onClick="ga('send', 'event', 'TopBannerSciCom', 'click','SciComTopTuition',1.00, {'nonInteraction': 1});" ><img width="100%" style="border: 0px" src="http://ads.esfiya.com/wp-content/uploads/2015/08/tuition_top.jpg" alt="" /></a>
<img width=0 height=0 src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="ga('send', 'event', 'TopBannerSciCom', 'impression','SciComTopTuition',2.00, {'nonInteraction': 1});"/>
</div>
<div style="display: none;" id="top_2nd">
<a href="http://scicom.mv" onClick="ga('send', 'event', 'TopBannerSciCom', 'click','SciComTopEng',1.00, {'nonInteraction': 1});" ><img width="100%" style="border: 0px" src="http://ads.esfiya.com/wp-content/uploads/2015/08/eng_top.jpg" alt="" /></a>
<img width=0 height=0 src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="ga('send', 'event', 'TopBannerSciCom', 'impression','SciComTopEng',2.00, {'nonInteraction': 1});"/>
</div>
<div style="display: none;" id="top_3rd">
<a href="http://scicom.mv" onClick="ga('send', 'event', 'TopBannerAdHere', 'click','TopAdHere',1.00, {'nonInteraction': 1});" ><img width="100%" style="border: 0px" src="http://ads.esfiya.com/wp-content/uploads/2015/04/ad-here-red700x140.jpg" alt="" /></a>
<img width=0 height=0 src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="ga('send', 'event', 'TopBannerAdHere', 'impression','TopAdHere',2.00, {'nonInteraction': 1});"/>
</div>
</div>
All your advertisements are loaded, so all three onload scripts will take effect. They are only not visible because display: none.
You have to attack this impression event only to desired advertisement.
Math.random() < 0.5 ? value1 : value2 : value3;
Try this instead:
var options = ['top_1st','top_2nd','top_3rd'];
var chosenValue = options[Math.floor(Math.random()*options.length)];
Why do you use non-interaction flag on onclick event? it is users interaction, so it would be interactive event.
onClick="ga('send', 'event', 'TopBannerSciCom', 'click','SciComTopEng',1.00);"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.