Skip to content
Login Contact

Ads tracking setup for Google Publisher Tag events

Marfeel tracks ads automatically. However, if ad scripts load before the Marfeel SDK is ready, those ad events will not be captured. This happens when the script loading order causes ads to render before Marfeel initializes.

To ensure complete ad tracking, add the following Google Publisher Tag event listeners before any ad script on the page:

<script type="text/javascript">
window.googletag = window.googletag || { cmd: [] };
window.marfeel = window.marfeel || { cmd: [] };
googletag.cmd.push(function () {
googletag.pubads().addEventListener('slotRenderEnded', function (event) {
if (!event.isEmpty) {
window.marfeel.cmd.push(['compass', function (compass) {
compass.trackAdEvent('slotRenderEnded', event.slot);
}]);
}
});
googletag.pubads().addEventListener('slotVisibilityChanged', function (event) {
window.marfeel.cmd.push(['compass', function (compass) {
compass.trackAdEvent('slotVisibilityChanged', event.slot);
}]);
});
});
</script>

This queues ad events through window.marfeel.cmd, so Marfeel captures all slotRenderEnded and slotVisibilityChanged data even if the SDK has not finished loading yet.

To notify Marfeel that ads are counted by an external script, initialize the SDK with adsTrackingFromClient: true:

<script type="text/javascript">
function e(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],c=document.createElement("script");c.src=e,t?c.type="module":(c.async=!0,c.type="text/javascript",c.setAttribute("nomodule",""));var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(c,n)}function t(t,c,n){var a,o,r;null!==(a=t.marfeel)&&void 0!==a||(t.marfeel={}),null!==(o=(r=t.marfeel).cmd)&&void 0!==o||(r.cmd=[]),t.marfeel.config=n,t.marfeel.config.accountId=c;var i="https://sdk.mrf.io/statics";e("".concat(i,"/marfeel-sdk.js?id=").concat(c),!0),e("".concat(i,"/marfeel-sdk.es5.js?id=").concat(c),!1)}!function(e,c){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t(e,c,n)}(window,0
/* AccountId */, {adsTrackingFromClient:true} /* Config */);
</script>

Interstitial ads require explicit slot registration so Compass can identify and track them. Define the interstitial slot using googletag.defineOutOfPageSlot and pass it to compass.setInterstitialSlot:

<script type="text/javascript">
window.googletag = window.googletag || { cmd: [] };
window.marfeel = window.marfeel || { cmd: [] };
let interstitialSlot;
googletag.cmd.push(function () {
// Define a web interstitial ad slot.
interstitialSlot = googletag.defineOutOfPageSlot('YOUR_INTERSTITIAL_SLOT', googletag.enums.OutOfPageFormat.INTERSTITIAL);
if (interstitialSlot) {
interstitialSlot.addService(googletag.pubads());
window.marfeel.cmd.push(['compass', function (compass) {
compass.setInterstitialSlot(interstitialSlot);
}]);
}
});
</script>

This method can be combined with the personalized ad tracking script described above.

For AmpFirst sites using the JavaScript tracker instead of amp-analytics, add the isAmpFirst flag to the SDK configuration to enable AMP ad tracking:

<script type="text/javascript">
function e(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],c=document.createElement("script");c.src=e,t?c.type="module":(c.async=!0,c.type="text/javascript",c.setAttribute("nomodule",""));var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(c,n)}function t(t,c,n){var a,o,r;null!==(a=t.marfeel)&&void 0!==a||(t.marfeel={}),null!==(o=(r=t.marfeel).cmd)&&void 0!==o||(r.cmd=[]),t.marfeel.config=n,t.marfeel.config.accountId=c;var i="https://sdk.mrf.io/statics";e("".concat(i,"/marfeel-sdk.js?id=").concat(c),!0),e("".concat(i,"/marfeel-sdk.es5.js?id=").concat(c),!1)}!function(e,c){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};t(e,c,n)}(window,0
/* AccountId */, {isAmpFirst:true} /* Config */);
</script>

For accurate ad tracking, include the data-vars-ad-slot parameter in the amp-ad tag to provide the slot name:

<amp-ad width="300" height="250" type="foo" ad-slot="SLOTNAME" data-vars-ad-slot="SLOTNAME">
<div placeholder>Loading ...</div>
</amp-ad>
Why are some ads not tracked by Marfeel?

If ad scripts load before the Marfeel SDK is ready, those ad events will not be captured. Adding Google Publisher Tag event listeners before any ad script ensures that all slotRenderEnded and slotVisibilityChanged events are queued and processed once the SDK initializes.

How do I enable client-side ads tracking in the Marfeel SDK?

Initialize the SDK with the adsTrackingFromClient: true configuration flag. This tells Marfeel that ad events are being captured by an external script rather than the default automatic tracking.

How do I track interstitial ads with Marfeel?

Define the interstitial slot using googletag.defineOutOfPageSlot with the INTERSTITIAL OutOfPageFormat, then pass the slot reference to compass.setInterstitialSlot so Compass can identify and track it correctly.