Search code examples
javascriptiframegoogle-dfp

How to add an extra classname on iframe, generated by DFP?


I have a DFP creative template that needs to work both on old and new site. So on the new site (which has its own advertisement component js), I'd like to add a classname "redesign" on the DFP iframe. How do I do it properly?

In conclusion, I'd like to have same creative template on DFP with extra classname for new site.


Solution

  • Solved it by using the plugin => https://github.com/mcountis/dfp-events

    So on redesign ad.js component, I add a new classname "redesign".

    
        window.googletag.cmd.push(function() {
            window.googletag.on('gpt-slot_rendered', function(e,level,message,service,slot) {
                var slotId = slot.getSlotId();
                var $slot = $('#' + slotId.getDomId());
    
                // DFP adds two iframes, one for calling scripts and one for displaying the ad. we want the one that is not hidden
                if ($slot.find('iframe:not([id*=hidden])')
                    .map(function() { return this.contentWindow.document; })
                    .find('body')
                    .children().length > 0
                ) {
                    $slot.find('iframe').contents().find('body').addClass('redesign');
                }
            });
        });