Advanced Implementation

Manual Ad Triggering

AMP IMA implementation offers a requestAd() method in which you can manually trigger ads, we do highly recommend to use VMAP ads in which the ad timing is configured already by the ad provider. There are some special cases in which you might require requestAd, here is an example.

Supposing that you want to trigger a single ad during a live stream based on whether user action or local time you can invoke the requestAd.

function loadAd(event) {
			  if (!amp.ads.getInProgress())
					amp.ads.requestAd();
			}

If you want to play or change the adTagUrl dynamically you can use player parameterization, in which you bind a specific player value with a variable scoped on page level or app.

//default adTag
window.adTag = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=xml_vmap1&unviewed_position_start=1&cust_params=sample_ar%3Dpremidpostpod&cmsid=496&vid=short_onecue&correlator="

var config = {
	plugins: {
		ima: {
      		resources: [
        		{src: "//imasdk.googleapis.com/js/sdkloader/ima3.js", debug: "//imasdk.googleapis.com/js/sdkloader/ima3_debug.js", type: "text/javascript", async: true},
        		{src: "${paths.plugins}ima/Ima.min.js", debug: "${paths.plugins}ima/Ima.js", type: "text/javascript", async: true}
      		],
		adTagUrl: "#{window.adTag}",
		vpaidMode: "enabled"
		}
	}
};

akamai.amp.AMP.create("akamai-media-player", config);

loadAd() {
    window.adTag = newAdTag; // here the new ad tag is set.

	if (!amp.ads.getInProgress())
		amp.ads.requestAd(); // call requestAd after new ad tag was set.
}

Once the adTagUrl was updated you can trigger requestAd() in order to request the new ad.