Search code examples
htmlimasdk

How to trigger HTML5 IMA SDK Click event manually?


I am using HTML5 IMA SDK, I would like to programmatically trigger the click event of the sdk or if that's not possible, I would like to extract the clickthrough URL. The clickTrackingElement that I am providing is an anchor tag. The end result that I want to achieve is an anchor tag that is capable of opening the link of the ima sdk ad. That anchor tag is outside the adContainer or the mainContainer in the following html used for setting up the ima sdk ads container and its backup player.

<div id="mainContainer">
    <div id="content">
        <video id="externalAdVideo"></video>
    </div>
    <div id="adContainer"></div>
</div>

I tried to activate the CustomPlayback and CustomClickTracking, by using the following code

this.adDisplayContainer = new google.ima.AdDisplayContainer(adContainer, videoElement, clickTrackingElement);

but then when I check if either's enabled by using the following code

console.log('ima.adsManager.isCustomPlaybackUsed: ' + ima.adsManager.isCustomPlaybackUsed());
console.log('ima.adsManager.isCustomClickTrackingUsed: ' + ima.adsManager.isCustomClickTrackingUsed());

both of them are returning false, am I missing something?


Solution

  • Looks like you can at least find the clickThroughUrl like this (tested with ima version 3.145.0):

    ima.adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, function (event) {
        var ad = event.getAd(),
                key = Object.keys(ad)[0] || null;
    
        if (key && ad[key]) {
            console.log('ClickThroughUrl: ' + ad[key].clickThroughUrl);
        }
    });