├── README.md └── dfp.gpt.logger.override.js /README.md: -------------------------------------------------------------------------------- 1 | # dfp-events 2 | 3 | For use with Google's Doubleclick for Publishers (DFP). Taps into dfp's logger function and triggers events. 4 | 5 | ## Methods: 6 | ### `googletag.on()` 7 | ```javascript 8 | googletag.on(events,data,callback); 9 | ``` 10 | * `events` - string containing a list of events to bind to. 11 | * `data` - (optional) data to be passed to the callback upon triggering, passed via arguments[0].data a la jQuery. 12 | * `callback` - function to be called upon one of the events occuring. 13 | 14 | ### `googletag.off()` 15 | ```javascript 16 | googletag.off(events,callback); 17 | ``` 18 | * `events` - string containing a list of events to remove callbacks from. 19 | * `callback` - (optional) specific callback function to be removed from googletag. 20 | 21 | ## Events: 22 | 23 | * gpt-google_js_loaded 24 | * gpt-gpt_fetch 25 | * gpt-gpt_fetched 26 | * gpt-page_load_complete 27 | * gpt-queue_start 28 | * gpt-service_add_slot 29 | * gpt-service_add_targeting 30 | * gpt-service_collapse_containers_enable 31 | * gpt-service_create 32 | * gpt-service_single_request_mode_enable 33 | * gpt-slot_create 34 | * gpt-slot_add_targeting 35 | * gpt-slot_fill 36 | * gpt-slot_fetch 37 | * gpt-slot_receiving 38 | * gpt-slot_render_delay 39 | * gpt-slot_rendering 40 | * gpt-slot_rendered 41 | 42 | ## Examples: 43 | 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. 44 | 45 | ### Bind callback to a DFP event 46 | ```javascript 47 | googletag.cmd.push(function () { 48 | 49 | // add class 'empty' to ad div if the inserted iframe document is, in fact, empty 50 | googletag.on("gpt-slot_rendered",function(e,level,message,service,slot,reference){ 51 | var slotId = slot.getSlotId(), 52 | $slot = $("#"+slotId.getDomId()); 53 | 54 | // DFP adds two iframes, one for calling scripts and one for displaying the ad. we want the one that is not hidden 55 | if($slot.find("iframe:not([id*=hidden])") 56 | .map(function () { return this.contentWindow.document; }) 57 | .find("body") 58 | .children().length == 0 59 | ){ 60 | $slot.addClass("empty"); 61 | } 62 | }); 63 | }); 64 | ``` 65 | 66 | ### Remove binding 67 | ```javascript 68 | googletag.cmd.push(function () { 69 | googletag.off("gpt-slot_rendered"); 70 | }); 71 | ``` 72 | -------------------------------------------------------------------------------- /dfp.gpt.logger.override.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Michael Countis 3 | 4 | MIT License: http://opensource.org/licenses/MIT 5 | */ 6 | 7 | (function(){ 8 | "use strict"; 9 | 10 | window.googletag = window.googletag || {}; 11 | window.googletag.cmd = window.googletag.cmd || []; 12 | 13 | googletag.cmd.push(function(){ 14 | 15 | if(googletag.hasOwnProperty("on") || googletag.hasOwnProperty("off") || googletag.hasOwnProperty("trigger") || googletag.hasOwnProperty("events")) 16 | return; 17 | 18 | var old_log = googletag.debug_log.log, 19 | events = [], 20 | addEvent = function(name,id,match){ 21 | events.push({ 22 | "name":name, 23 | "id":id, 24 | "match":match 25 | }); 26 | }; 27 | 28 | addEvent("gpt-google_js_loaded", 8, /Google service JS loaded/ig); 29 | addEvent("gpt-gpt_fetch", 46, /Fetching GPT implementation/ig); 30 | addEvent("gpt-gpt_fetched", 48, /GPT implementation fetched\./ig); 31 | addEvent("gpt-page_load_complete", 1, /Page load complete/ig); 32 | addEvent("gpt-queue_start", 31, /^Invoked queued function/ig); 33 | 34 | addEvent("gpt-service_add_slot", 40, /Associated ([\w]*) service with slot ([\/\w]*)/ig); 35 | addEvent("gpt-service_add_targeting", 88, /Setting targeting attribute ([\w]*) with value ([\w\W]*) for service ([\w]*)/ig); 36 | addEvent("gpt-service_collapse_containers_enable", 78, /Enabling collapsing of containers when there is no ad content/ig); 37 | addEvent("gpt-service_create", 35, /Created service: ([\w]*)/ig); 38 | addEvent("gpt-service_single_request_mode_enable", 63, /Using single request mode to fetch ads/ig); 39 | 40 | addEvent("gpt-slot_create", 2, /Created slot: ([\/\w]*)/ig); 41 | addEvent("gpt-slot_add_targeting", 17, /Setting targeting attribute ([\w]*) with value ([\w\W]*) for slot ([\/\w]*)/ig); 42 | addEvent("gpt-slot_fill", 50, /Calling fillslot/ig); 43 | addEvent("gpt-slot_fetch", 3, /Fetching ad for slot ([\/\w]*)/ig); 44 | addEvent("gpt-slot_receiving", 4, /Receiving ad for slot ([\/\w]*)/ig); 45 | addEvent("gpt-slot_render_delay", 53, /Delaying rendering of ad slot ([\/\w]*) pending loading of the GPT implementation/ig); 46 | addEvent("gpt-slot_rendering", 5, /^Rendering ad for slot ([\/\w]*)/ig); 47 | addEvent("gpt-slot_rendered", 6, /Completed rendering ad for slot ([\/\w]*)/ig); 48 | 49 | googletag.events = googletag.events || {}; 50 | 51 | googletag.on = function(events,op_arg0/*data*/,op_arg1/*callback*/){ 52 | if(!op_arg0) 53 | return this; 54 | 55 | events = events.split(" "); 56 | 57 | var data = op_arg1 ? op_arg0 : undefined, 58 | callback = op_arg1 || op_arg0, 59 | ei = 0,e = ''; 60 | 61 | callback.data = data; 62 | 63 | for(e = events[ei = 0]; ei < events.length; e = events[++ei]) 64 | (this.events[e] = this.events[e] || []).push(callback); 65 | 66 | return this; 67 | }; 68 | 69 | 70 | googletag.off = function(events,handler){ 71 | events = events.split(" "); 72 | var ei = 0,e = "", 73 | fi = 0,f = function(){}; 74 | 75 | for(e = events[ei]; ei < events.length; e = events[++ei]){ 76 | if(!this.events.hasOwnProperty(e)) 77 | continue; 78 | 79 | if(!handler){ 80 | delete this.events[e]; 81 | continue; 82 | } 83 | 84 | fi = this.events[e].length - 1; 85 | for(f = this.events[e][fi]; fi >= 0; f = this.events[e][--fi]) 86 | if(f == handler) 87 | this.events[e].splice(fi,1); 88 | if(this.events[e].length === 0) 89 | delete this.events[e]; 90 | } 91 | 92 | return this; 93 | }; 94 | 95 | 96 | googletag.trigger = function(event,parameters){ 97 | 98 | if(!this.events[event] || this.events[event].length === 0) 99 | return this; 100 | 101 | var parameters = parameters || [], 102 | fi = 0,f = this.events[event][fi]; 103 | 104 | for(fi,f;fi < this.events[event].length;f = this.events[event][++fi]) 105 | if(f.apply(this,[{data:f.data}].concat(parameters)) === false) 106 | break; 107 | 108 | return this; 109 | }; 110 | 111 | 112 | googletag.debug_log.log = function(level,message,service,slot,reference){ 113 | if (message && message.getMessageId && typeof (message.getMessageId()) == 'number') { 114 | var args = Array.prototype.slice.call(arguments), 115 | e = 0; 116 | for(e; e < events.length; e++) 117 | if(events[e].id === message.getMessageId()) 118 | googletag.trigger(events[e].name,args); 119 | } 120 | return old_log.apply(this,arguments); 121 | }; 122 | 123 | 124 | }); 125 | })(); 126 | --------------------------------------------------------------------------------