Shashank Vinayak
Shashank Vinayak

Reputation: 163

Way to know the type of ads being served from Google ad manager while using Google Publisher Tag

Is there a way to know the type of ad(Native/Banner ad) being served in each ad slot from Google ad manager on a website? I am using a code snippet similar to what is provided in the gpt documentation:

<script>
      window.googletag = window.googletag || {cmd: []};
      googletag.cmd.push(function() {
        googletag
            .defineSlot(
                '/6355419/Travel/Europe/France/Paris', [300, 250], 'div-id')
            .addService(googletag.pubads());
        googletag.enableServices();
      });
    </script>

<div id="div-id" style="width: 300px; height: 250px;">
      <script>
        googletag.cmd.push(function() {
          googletag.display('div-id');
        });
      </script>
    </div>

Is there any event that I can listen to, in order to know the ad type?

Upvotes: 0

Views: 852

Answers (1)

rabsom
rabsom

Reputation: 859

The Google Publisher Tag API enables you to retrieve the creativeTemplateId used for each rendered slot.

As long as you know your templates ids, you should be able to do specific actions :

<script>
window.googletag = window.googletag || {cmd: []};
googletag.cmd.push(function() {
  googletag
    .defineSlot('/6355419/Travel/Europe/France/Paris', [300, 250], 'div-id')
    .addService(googletag.pubads().addEventListener('slotRenderEnded',function(event) {
       var slot = event.slot;
       console.group('Slot', slot.getSlotElementId(), 'finished rendering.')
       // Log details of the rendered ad.
       console.log('Creative Template ID:', event.creativeTemplateId);
       console.groupEnd();
       if(event.creativeTemplateId === yourIdentifiedTemplateId) {
         //your specific action
       }
      }));
  );
googletag.enableServices();
});
</script>

<div id="div-id" style="width: 300px; height: 250px;">
<script>
  googletag.cmd.push(function() {
    googletag.display('div-id');
  });
</script>
</div>

Concerning yourIdentifiedTemplateId :

  • it is an integer (not a string)
  • you will find it in your Google Ad Manager UI (in "Native ad formats" page for native, in your creative template URL for custom display templates)

Full documentation :

Hope this helps ;)

Upvotes: 1

Related Questions