GithubHelp home page GithubHelp logo

mcountis / dfp-events Goto Github PK

View Code? Open in Web Editor NEW
133.0 13.0 22.0 124 KB

For use with Google's Doubleclick for Publishers (DFP). Taps into dfp's logger function and fires triggers events using jQuery.

JavaScript 100.00%

dfp-events's Introduction

dfp-events

For use with Google's Doubleclick for Publishers (DFP). Taps into dfp's logger function and triggers events.

Methods:

googletag.on()

googletag.on(events,data,callback);
  • events - string containing a list of events to bind to.
  • data - (optional) data to be passed to the callback upon triggering, passed via arguments[0].data a la jQuery.
  • callback - function to be called upon one of the events occuring.

googletag.off()

googletag.off(events,callback);
  • events - string containing a list of events to remove callbacks from.
  • callback - (optional) specific callback function to be removed from googletag.

Events:

  • gpt-google_js_loaded
  • gpt-gpt_fetch
  • gpt-gpt_fetched
  • gpt-page_load_complete
  • gpt-queue_start
  • gpt-service_add_slot
  • gpt-service_add_targeting
  • gpt-service_collapse_containers_enable
  • gpt-service_create
  • gpt-service_single_request_mode_enable
  • gpt-slot_create
  • gpt-slot_add_targeting
  • gpt-slot_fill
  • gpt-slot_fetch
  • gpt-slot_receiving
  • gpt-slot_render_delay
  • gpt-slot_rendering
  • gpt-slot_rendered

Examples:

Notice I use jQuery to cause changes to the DOM but not to bind or unbind the callbacks to the googletag object. Also the method calls are inside a googletag.cmd.push call to ensure the api is up and running.

Bind callback to a DFP event

googletag.cmd.push(function () {
	
	// add class 'empty' to ad div if the inserted iframe document is, in fact, empty
	googletag.on("gpt-slot_rendered",function(e,level,message,service,slot,reference){
		var	slotId = slot.getSlotId(),
			$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.addClass("empty");
		}
	});
});

Remove binding

googletag.cmd.push(function () {
	googletag.off("gpt-slot_rendered");
});

dfp-events's People

Contributors

lukeshumard avatar mcountis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dfp-events's Issues

[REQUEST] publish to npm

useful enhancement to DFP. any chance we can see this on npm? would be easier to manage that way as a 3rd party dependency rather than copying alongside application code.

Error message.search is not a function

Hi,

Yesterday I start getting this error at this line:

if(message.search(events[e].match) > -1)

I printed the object "message" and there is no "Search" function available.
Please help.
Thank you in advance.

Events not firing on Chrome and IE

Hi and thank you for this code.
I just set it up on my development environment, and I was very excited to see it working on Firefox!

But when I tested on Chrome and IE8, the events were not firing. This is how the whole of my DFP related JS looks like:

googletag.cmd.push(function() {

var arr = document.getElementsByTagName('div');
for (var i=0; i<arr.length; i++)
{
    if (arr[i].className == 'gpt-ad')
    {
        var slot_name = arr[i].getAttribute('slot_name');
        var id = arr[i].getAttribute('id');
        var width = parseInt(arr[i].getAttribute('width'));
        var height = parseInt(arr[i].getAttribute('height'));
        var targeting_key = arr[i].getAttribute('targeting_key');
        var targeting_values = arr[i].getAttribute('targeting_values').split("|");
        googletag.defineSlot(slot_name, [width, height], id).addService(googletag.pubads()).setTargeting(targeting_key, targeting_values);
    }
}

googletag.pubads().collapseEmptyDivs();
googletag.enableServices();

var arr = document.getElementsByTagName('div');
for (var i=0; i<arr.length; i++)
{
    if (arr[i].className == 'gpt-ad')
    {
        var id = arr[i].getAttribute('id');
        googletag.cmd.push(function() { googletag.display(id); });
    }
}

if (googletag.on !== undefined && $ !== undefined) {
    googletag.on("gpt-slot_rendered", function(e, level, message, service, slot, reference) {
        var slotId = slot.getSlotId();
        var $slot = $("#" + slotId.getDomId());

        if($slot.find("iframe:not([id*=hidden])")
            .map(function() {
                return this.contentWindow.document;
            })
            .find("body").children().length > 0) {
            $slot.closest('.advertisement').show();
        }
    });
}

});

My ads are in a container called ".advertisement", and it's display:none by default. Then when an ad is rendered, I'm calling .show() on it.

This is because I don't want to display an empty box when there's no ad booked, and I don't have house ads to run.

Anyway, this works great in Firefox, but when I checked in Chrome, the event wasn't firing. I put a breakpoint at $slot.closest('.advertisement').show(); and nothing happened. Same in IE8.

Could you please help me figure out what's wrong?
Thank you!

[DO NOT USE] Library will no longer work after Jan 21, 2019

Hello,

I'm an engineer at Google, on the team that maintains gpt.js. Please do not use this library, or rely on hooking into googletag.event_log in any way. It's being removed as part of a code cleanup our team is doing. Any code accessing that property will likely break, since that key will no longer exist.

googletag.event_log is not a public API, and was never meant to be used as such. See the supported API docs here.

Here is the list of events which we officially support:

  • impressionViewable
  • slotVisibilityChanged
  • slotOnload
  • slotRenderEnded

If you have a use case which is not supported by any of these existing events, please file product feedback using the "Send Feedback" button (located in the upper-right corner of our docs page)

Logging doesn't seem to work in <= IE8

Hey guys,

It seems calling the extended googletag.debug_log.log() method gets called once and then never again in browsers <= IE8

Any ideas, anyone else seen this?

Licensing

I noticed you haven't declared any license, yet, can you clarify it?

Ads rendering response time similar to ?googfc

Is it possible to get the loading time and rendering time of creatives in the page using gpt-slot_render_delay or gpt-slot_rendering events ? I have totally four adunits in the page. I wanted to log all the adunit's rendering and loading response time similar to google publisher console's ?googfc does.

Excellent work!

Your code is great -- very clever solution; thank you for sharing it. We're using it to manage the visibility of responsive ad units. At different breakpoints we load different ad sizes, and we hide/show them as appropriate. To keep the number of impressions down (and keep the payload down for the user), we defer the loading of the various ad sizes until they're needed, but GPT forces the visibility of the ad after the async load is complete, which interferes with our visibility management code. Tapping into the rendered event is a perfect way to maintain control over which ad is showing at a given breakpoint!

Events should be built into GPT -- have you spoken with anybody at Google about that? My biggest concern is how fragile this all will be if Google changes their logging tomorrow or next week.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.