Recommended Ads Tracking method

Ads are tracked automatically by Compass, but in some cases and depending on the order that you are loading the scripts, some ads can be loaded way before Compass is ready and won’t be tracked.
In order to address that what is recommended is to create 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 compass (even if it’s not loaded yet) will have all this information.
And in order to tell Compass that ads are counted by an external script we have to add adsTrackingFromClient: true to the configuration:

<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>
Remember to replace the `000` with your own account ID!

It is the same ID that you use for AMP and FIB instrumentation.

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.

Ads AmpFirst

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>

In order to track properly the ads is recomended to add the parameter data-vars-ad-slot to the amp-ad with 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>