REFACTOR: some changes to be closer to example implementation

https://support.google.com/admanager/answer/4578089?hl=en

* unique div ID for every ad slot
* setTargeting before calling addService
* don't destroySlots async (no examples of correct way to use this...)
This commit is contained in:
Neil Lalonde 2019-06-10 15:43:48 -04:00
parent 0432256adb
commit 3a04f95916
1 changed files with 52 additions and 41 deletions

View File

@ -6,10 +6,14 @@ import {
} from "ember-addons/ember-computed-decorators"; } from "ember-addons/ember-computed-decorators";
import loadScript from "discourse/lib/load-script"; import loadScript from "discourse/lib/load-script";
var currentUser = Discourse.User.current(), let _loaded = false,
_loaded = false,
_promise = null, _promise = null,
ads = {}; ads = {},
nextSlotNum = 1;
function getNextSlotNum() {
return nextSlotNum++;
}
function splitWidthInt(value) { function splitWidthInt(value) {
var str = value.substring(0, 3); var str = value.substring(0, 3);
@ -23,7 +27,7 @@ function splitHeightInt(value) {
// This creates an array for the values of the custom targeting key // This creates an array for the values of the custom targeting key
function valueParse(value) { function valueParse(value) {
var final = value.replace(/ /g, ""); let final = value.replace(/ /g, "");
final = final.replace(/['"]+/g, ""); final = final.replace(/['"]+/g, "");
final = final.split(","); final = final.split(",");
return final; return final;
@ -31,32 +35,21 @@ function valueParse(value) {
// This creates an array for the key of the custom targeting key // This creates an array for the key of the custom targeting key
function keyParse(word) { function keyParse(word) {
var key = word; let key = word;
key = key.replace(/['"]+/g, ""); key = key.replace(/['"]+/g, "");
key = key.split("\n"); key = key.split("\n");
return key; return key;
} }
// This sets the key and value for custom targeting
var Foo = function(key, value, adslot) {
this.locationKey = key;
this.locationValue = value;
this.adslot = adslot;
};
Foo.prototype.bar = function() {
if (this.locationKey) {
this.adslot.setTargeting(this.locationKey, this.locationValue);
}
};
// This should call adslot.setTargeting(key for that location, value for that location) // This should call adslot.setTargeting(key for that location, value for that location)
function custom_targeting(key_array, value_array, location) { function custom_targeting(key_array, value_array, adSlot) {
var f;
for (var i = 0; i < key_array.length; i++) { for (var i = 0; i < key_array.length; i++) {
var wordValue = valueParse(value_array[i]); if (key_array[i]) {
f = new Foo(key_array[i], wordValue, location); adSlot.setTargeting(
f.bar(); key_array[i],
valueParse(value_array[i])
)
}
} }
} }
@ -129,7 +122,7 @@ function getWidthAndHeight(placement, settings, isMobile) {
}; };
} }
function defineSlot(divId, placement, settings, isMobile) { function defineSlot(divId, placement, settings, isMobile, categoryTarget) {
if (!settings.dfp_publisher_id) { if (!settings.dfp_publisher_id) {
return; return;
} }
@ -152,30 +145,37 @@ function defineSlot(divId, placement, settings, isMobile) {
"/" + settings.dfp_publisher_id + "/" + settings[config.code], "/" + settings.dfp_publisher_id + "/" + settings[config.code],
[size.width, size.height], [size.width, size.height],
divId divId
) );
.addService(window.googletag.pubads());
custom_targeting( custom_targeting(
keyParse(settings[config.targeting_keys]), keyParse(settings[config.targeting_keys]),
keyParse(settings[config.targeting_values]), keyParse(settings[config.targeting_values]),
ad ad
); );
if (ad) { if (categoryTarget) {
ads[divId] = { ad: ad, width: size.width, height: size.height }; ad.setTargeting("discourse-category", categoryTarget);
return ads[divId];
} }
ad.addService(window.googletag.pubads());
ads[divId] = { ad: ad, width: size.width, height: size.height };
return ads[divId];
} }
function destroySlot(divId) { function destroySlot(divId) {
if (ads[divId] && window.googletag) { if (ads[divId] && window.googletag) {
window.googletag.cmd.push(function() { window.googletag.destroySlots([ads[divId].ad]);
window.googletag.destroySlots([ads[divId].ad]); delete ads[divId];
delete ads[divId];
});
} }
} }
function loadGoogle() { function loadGoogle() {
/**
* Refer to this article for help:
* https://support.google.com/admanager/answer/4578089?hl=en
*/
if (_loaded) { if (_loaded) {
return Ember.RSVP.resolve(); return Ember.RSVP.resolve();
} }
@ -195,12 +195,18 @@ function loadGoogle() {
} }
window.googletag.cmd.push(function() { window.googletag.cmd.push(function() {
// Infinite scroll requires SRA:
window.googletag.pubads().enableSingleRequest(); window.googletag.pubads().enableSingleRequest();
window.googletag.pubads().disableInitialLoad(); // we always use refresh() to fetch the ads
// we always use refresh() to fetch the ads:
window.googletag.pubads().disableInitialLoad();
window.googletag.enableServices(); window.googletag.enableServices();
}); });
}); });
window.googletag = window.googletag || {cmd: []};
return _promise; return _promise;
} }
@ -212,10 +218,11 @@ export default AdComponent.extend({
@computed("placement", "postNumber") @computed("placement", "postNumber")
divId(placement, postNumber) { divId(placement, postNumber) {
let slotNum = getNextSlotNum();
if (postNumber) { if (postNumber) {
return `div-gpt-ad-${placement}-${postNumber}`; return `div-gpt-ad-${slotNum}-${placement}-${postNumber}`;
} else { } else {
return `div-gpt-ad-${placement}`; return `div-gpt-ad-${slotNum}-${placement}`;
} }
}, },
@ -302,13 +309,12 @@ export default AdComponent.extend({
self.get("divId"), self.get("divId"),
self.get("placement"), self.get("placement"),
self.siteSettings, self.siteSettings,
self.site.mobileView self.site.mobileView,
self.get("currentCategorySlug") || "0"
); );
if (slot && slot.ad) { if (slot && slot.ad) {
slot.ad.setTargeting( // Display has to be called before refresh
"discourse-category", // and after the slot div is in the page.
self.get("currentCategorySlug") || "0"
);
window.googletag.display(self.get("divId")); window.googletag.display(self.get("divId"));
window.googletag.pubads().refresh([slot.ad]); window.googletag.pubads().refresh([slot.ad]);
} }
@ -318,6 +324,11 @@ export default AdComponent.extend({
willRender() { willRender() {
this._super(...arguments); this._super(...arguments);
if (!this.get("showAd")) {
return;
}
let size = getWidthAndHeight( let size = getWidthAndHeight(
this.get("placement"), this.get("placement"),
this.siteSettings, this.siteSettings,