Recommended Ads Tracking method

Ads are tracked automatically by Marfeel. In some situations depending on the order that you are loading the scripts, some ads might be loaded way before Marfeel is ready and won’t be tracked.

To address this, it is recommended to add the following script before any ad script:

<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 way when ads are loaded, Marfeel (even if it’s not loaded yet) will be able to track all the information.

In order to notify Marfeel that ads are counted by an external script you have to 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 tracking

Interstitial ads need special coding for proper tracking. Compass needs to know which slot contains the interstitial, so we need to set it up.

<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>

Obviously, we can use this method in conjunction with personalized ad tracking, as mentioned in previous chapters.

How to accurately track Ads in AmpFirst Pages

If your site is AmpFirst and you are using the javascript tracker instead of the amp-analytics one you’ll need to add the isAmpFirst flag to the configuration in order to track amp-ads:

<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 proper ad trackingd, it’s recomended to include the data-vars-ad-slot parameter in the amp-ad tag, providing the slotname information.

<amp-ad width="300" height="250" type="foo" ad-slot="SLOTNAME" data-vars-ad-slot="SLOTNAME">
	 <div placeholder>Loading ...</div>
</amp-ad>