2019-04-18 17:52:59 -04:00
|
|
|
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
|
2019-04-18 14:10:56 -04:00
|
|
|
import {
|
|
|
|
default as computed,
|
|
|
|
observes,
|
|
|
|
on
|
|
|
|
} from "ember-addons/ember-computed-decorators";
|
2018-10-22 14:49:32 -04:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2016-04-21 13:01:15 -04:00
|
|
|
var currentUser = Discourse.User.current(),
|
2018-10-22 14:49:32 -04:00
|
|
|
_loaded = false,
|
|
|
|
_promise = null,
|
|
|
|
ads = {};
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2015-08-10 00:04:21 -04:00
|
|
|
function splitWidthInt(value) {
|
2018-10-22 14:49:32 -04:00
|
|
|
var str = value.substring(0, 3);
|
|
|
|
return str.trim();
|
2015-08-10 00:04:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function splitHeightInt(value) {
|
2018-10-22 14:49:32 -04:00
|
|
|
var str = value.substring(4, 7);
|
|
|
|
return str.trim();
|
2015-08-10 00:04:21 -04:00
|
|
|
}
|
2015-08-30 22:21:43 -04:00
|
|
|
|
2015-08-26 01:35:30 -04:00
|
|
|
// This creates an array for the values of the custom targeting key
|
|
|
|
function valueParse(value) {
|
2018-10-22 14:49:32 -04:00
|
|
|
var final = value.replace(/ /g, "");
|
|
|
|
final = final.replace(/['"]+/g, "");
|
|
|
|
final = final.split(",");
|
2015-08-26 01:35:30 -04:00
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
2015-08-30 22:21:43 -04:00
|
|
|
// This creates an array for the key of the custom targeting key
|
|
|
|
function keyParse(word) {
|
|
|
|
var key = word;
|
2018-10-22 14:49:32 -04:00
|
|
|
key = key.replace(/['"]+/g, "");
|
2015-08-30 22:21:43 -04:00
|
|
|
key = key.split("\n");
|
2015-10-15 13:58:49 -04:00
|
|
|
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)
|
|
|
|
function custom_targeting(key_array, value_array, location) {
|
|
|
|
var f;
|
|
|
|
for (var i = 0; i < key_array.length; i++) {
|
|
|
|
var wordValue = valueParse(value_array[i]);
|
|
|
|
f = new Foo(key_array[i], wordValue, location);
|
|
|
|
f.bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 11:15:40 -05:00
|
|
|
const DESKTOP_SETTINGS = {
|
|
|
|
"topic-list-top": {
|
|
|
|
code: "dfp_topic_list_top_code",
|
|
|
|
sizes: "dfp_topic_list_top_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_topic_list_top_key_code",
|
|
|
|
targeting_values: "dfp_target_topic_list_top_value_code"
|
|
|
|
},
|
|
|
|
"topic-above-post-stream": {
|
|
|
|
code: "dfp_topic_above_post_stream_code",
|
|
|
|
sizes: "dfp_topic_above_post_stream_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_topic_above_post_stream_key_code",
|
|
|
|
targeting_values: "dfp_target_topic_above_post_stream_value_code"
|
|
|
|
},
|
|
|
|
"topic-above-suggested": {
|
|
|
|
code: "dfp_topic_above_suggested_code",
|
|
|
|
sizes: "dfp_topic_above_suggested_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_topic_above_suggested_key_code",
|
|
|
|
targeting_values: "dfp_target_topic_above_suggested_value_code"
|
|
|
|
},
|
|
|
|
"post-bottom": {
|
|
|
|
code: "dfp_post_bottom_code",
|
2019-03-20 11:22:47 -04:00
|
|
|
sizes: "dfp_post_bottom_ad_sizes",
|
2019-02-22 11:15:40 -05:00
|
|
|
targeting_keys: "dfp_target_post_bottom_key_code",
|
|
|
|
targeting_values: "dfp_target_post_bottom_value_code"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MOBILE_SETTINGS = {
|
|
|
|
"topic-list-top": {
|
|
|
|
code: "dfp_mobile_topic_list_top_code",
|
|
|
|
sizes: "dfp_mobile_topic_list_top_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_topic_list_top_key_code",
|
|
|
|
targeting_values: "dfp_target_topic_list_top_value_code"
|
|
|
|
},
|
|
|
|
"topic-above-post-stream": {
|
|
|
|
code: "dfp_mobile_topic_above_post_stream_code",
|
|
|
|
sizes: "dfp_mobile_topic_above_post_stream_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_topic_above_post_stream_key_code",
|
|
|
|
targeting_values: "dfp_target_topic_above_post_stream_value_code"
|
|
|
|
},
|
|
|
|
"topic-above-suggested": {
|
|
|
|
code: "dfp_mobile_topic_above_suggested_code",
|
|
|
|
sizes: "dfp_mobile_topic_above_suggested_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_topic_above_suggested_key_code",
|
|
|
|
targeting_values: "dfp_target_topic_above_suggested_value_code"
|
|
|
|
},
|
|
|
|
"post-bottom": {
|
|
|
|
code: "dfp_mobile_post_bottom_code",
|
|
|
|
sizes: "dfp_mobile_post_bottom_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_post_bottom_key_code",
|
|
|
|
targeting_values: "dfp_target_post_bottom_value_code"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 12:10:39 -05:00
|
|
|
function getWidthAndHeight(placement, settings, isMobile) {
|
|
|
|
let config;
|
|
|
|
|
|
|
|
if (isMobile) {
|
|
|
|
config = MOBILE_SETTINGS[placement];
|
|
|
|
} else {
|
|
|
|
config = DESKTOP_SETTINGS[placement];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
width: parseInt(splitWidthInt(settings[config.sizes])),
|
|
|
|
height: parseInt(splitHeightInt(settings[config.sizes]))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-21 18:09:46 -04:00
|
|
|
function defineSlot(divId, placement, settings, isMobile) {
|
2016-11-21 15:55:03 -05:00
|
|
|
if (!settings.dfp_publisher_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:10:39 -05:00
|
|
|
let ad, config;
|
|
|
|
let size = getWidthAndHeight(placement, settings, isMobile);
|
2015-10-15 13:58:49 -04:00
|
|
|
|
2016-04-21 18:09:46 -04:00
|
|
|
if (ads[divId]) {
|
|
|
|
return ads[divId];
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
|
|
|
|
2019-02-22 11:15:40 -05:00
|
|
|
if (isMobile) {
|
|
|
|
config = MOBILE_SETTINGS[placement];
|
|
|
|
} else {
|
|
|
|
config = DESKTOP_SETTINGS[placement];
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
|
|
|
|
2019-02-22 11:15:40 -05:00
|
|
|
ad = window.googletag
|
|
|
|
.defineSlot(
|
|
|
|
"/" + settings.dfp_publisher_id + "/" + settings[config.code],
|
2019-02-22 12:10:39 -05:00
|
|
|
[size.width, size.height],
|
2019-02-22 11:15:40 -05:00
|
|
|
divId
|
|
|
|
)
|
|
|
|
.addService(window.googletag.pubads());
|
|
|
|
custom_targeting(
|
|
|
|
keyParse(settings[config.targeting_keys]),
|
|
|
|
keyParse(settings[config.targeting_values]),
|
|
|
|
ad
|
|
|
|
);
|
|
|
|
|
2016-04-21 13:01:15 -04:00
|
|
|
if (ad) {
|
2019-02-22 12:10:39 -05:00
|
|
|
ads[divId] = { ad: ad, width: size.width, height: size.height };
|
2016-04-21 18:09:46 -04:00
|
|
|
return ads[divId];
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
2015-08-30 22:21:43 -04:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:26:14 -04:00
|
|
|
function destroySlot(divId) {
|
|
|
|
if (ads[divId] && window.googletag) {
|
2018-10-22 14:49:32 -04:00
|
|
|
window.googletag.cmd.push(function() {
|
2016-05-16 14:26:14 -04:00
|
|
|
window.googletag.destroySlots([ads[divId].ad]);
|
|
|
|
delete ads[divId];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 12:11:29 -05:00
|
|
|
function loadGoogle() {
|
2015-07-24 00:01:47 -04:00
|
|
|
if (_loaded) {
|
|
|
|
return Ember.RSVP.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_promise) {
|
|
|
|
return _promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The boilerplate code
|
2018-10-22 14:49:32 -04:00
|
|
|
var dfpSrc =
|
|
|
|
("https:" === document.location.protocol ? "https:" : "http:") +
|
|
|
|
"//www.googletagservices.com/tag/js/gpt.js";
|
2015-07-24 00:01:47 -04:00
|
|
|
_promise = loadScript(dfpSrc, { scriptTag: true }).then(function() {
|
|
|
|
_loaded = true;
|
|
|
|
if (window.googletag === undefined) {
|
2018-10-22 14:49:32 -04:00
|
|
|
console.log("googletag is undefined!");
|
2015-07-24 00:01:47 -04:00
|
|
|
}
|
|
|
|
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.cmd.push(function() {
|
|
|
|
window.googletag.pubads().enableSingleRequest();
|
|
|
|
window.googletag.pubads().disableInitialLoad(); // we always use refresh() to fetch the ads
|
|
|
|
window.googletag.enableServices();
|
2015-07-24 00:01:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return _promise;
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
export default AdComponent.extend({
|
2018-10-22 14:49:32 -04:00
|
|
|
classNameBindings: ["adUnitClass"],
|
|
|
|
classNames: ["google-dfp-ad"],
|
2015-07-24 00:01:47 -04:00
|
|
|
loadedGoogletag: false,
|
2015-10-15 13:58:49 -04:00
|
|
|
refreshOnChange: null,
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("placement", "postNumber")
|
|
|
|
divId(placement, postNumber) {
|
|
|
|
if (postNumber) {
|
|
|
|
return `div-gpt-ad-${placement}-${postNumber}`;
|
2016-04-21 18:09:46 -04:00
|
|
|
} else {
|
2019-04-18 14:10:56 -04:00
|
|
|
return `div-gpt-ad-${placement}`;
|
2016-04-21 18:09:46 -04:00
|
|
|
}
|
2019-04-18 14:10:56 -04:00
|
|
|
},
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("placement", "showAd")
|
|
|
|
adUnitClass(placement, showAd) {
|
|
|
|
return showAd ? `dfp-ad-${placement}` : "";
|
|
|
|
},
|
2016-04-21 14:59:59 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("width", "height")
|
|
|
|
adWrapperStyle(w, h) {
|
|
|
|
return `width: ${w}px; height: ${h}px;`.htmlSafe();
|
|
|
|
},
|
2015-08-19 19:34:52 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("width")
|
|
|
|
adTitleStyleMobile(w) {
|
|
|
|
return `width: ${w}px;`.htmlSafe();
|
|
|
|
},
|
2015-10-28 19:51:53 -04:00
|
|
|
|
2019-06-05 15:52:52 -04:00
|
|
|
@computed(
|
|
|
|
"showToTrustLevel",
|
|
|
|
"showToGroups",
|
|
|
|
"showAfterPost",
|
|
|
|
"showOnCurrentPage"
|
|
|
|
)
|
|
|
|
showAd(showToTrustLevel, showToGroups, showAfterPost, showOnCurrentPage) {
|
2018-10-22 14:49:32 -04:00
|
|
|
return (
|
2019-04-18 17:52:59 -04:00
|
|
|
this.siteSettings.dfp_publisher_id &&
|
2019-04-18 14:10:56 -04:00
|
|
|
showToTrustLevel &&
|
2019-04-18 17:52:59 -04:00
|
|
|
showToGroups &&
|
2019-06-05 15:52:52 -04:00
|
|
|
showAfterPost &&
|
|
|
|
showOnCurrentPage
|
2018-10-22 14:49:32 -04:00
|
|
|
);
|
2019-04-18 14:10:56 -04:00
|
|
|
},
|
2016-11-21 15:55:03 -05:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("currentUser.trust_level")
|
|
|
|
showToTrustLevel(trustLevel) {
|
2018-10-22 14:49:32 -04:00
|
|
|
return !(
|
2019-04-18 17:52:59 -04:00
|
|
|
trustLevel && trustLevel > this.siteSettings.dfp_through_trust_level
|
2018-10-22 14:49:32 -04:00
|
|
|
);
|
2019-04-18 14:10:56 -04:00
|
|
|
},
|
2015-08-19 19:34:52 -04:00
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
@computed("postNumber")
|
|
|
|
showAfterPost(postNumber) {
|
|
|
|
if (!postNumber) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isNthPost(parseInt(this.siteSettings.dfp_nth_post_code));
|
|
|
|
},
|
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@observes("refreshOnChange")
|
|
|
|
refreshAd() {
|
2018-10-22 14:49:32 -04:00
|
|
|
var slot = ads[this.get("divId")];
|
|
|
|
if (!(slot && slot.ad)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-15 13:58:49 -04:00
|
|
|
|
2016-04-21 13:01:15 -04:00
|
|
|
var self = this,
|
2018-10-22 14:49:32 -04:00
|
|
|
ad = slot.ad;
|
2015-10-27 16:42:36 -04:00
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
if (this.get("loadedGoogletag") && this.get("refreshOnChange")) {
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.cmd.push(function() {
|
2018-10-22 14:49:32 -04:00
|
|
|
ad.setTargeting(
|
|
|
|
"discourse-category",
|
|
|
|
self.get("category") ? self.get("category") : "0"
|
|
|
|
);
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.pubads().refresh([ad]);
|
2015-10-15 13:58:49 -04:00
|
|
|
});
|
|
|
|
}
|
2019-04-18 14:10:56 -04:00
|
|
|
},
|
2015-10-15 13:58:49 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@on("didInsertElement")
|
|
|
|
_initGoogleDFP() {
|
2018-10-22 14:49:32 -04:00
|
|
|
if (!this.get("showAd")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-21 15:55:03 -05:00
|
|
|
|
2015-07-24 00:01:47 -04:00
|
|
|
var self = this;
|
2019-02-22 11:15:40 -05:00
|
|
|
loadGoogle(this.siteSettings).then(function() {
|
|
|
|
self.set("loadedGoogletag", true);
|
|
|
|
window.googletag.cmd.push(function() {
|
|
|
|
let slot = defineSlot(
|
|
|
|
self.get("divId"),
|
|
|
|
self.get("placement"),
|
|
|
|
self.siteSettings,
|
|
|
|
self.site.mobileView
|
|
|
|
);
|
|
|
|
if (slot && slot.ad) {
|
|
|
|
slot.ad.setTargeting(
|
|
|
|
"discourse-category",
|
|
|
|
self.get("category") ? self.get("category") : "0"
|
2018-10-22 14:49:32 -04:00
|
|
|
);
|
2019-02-22 11:15:40 -05:00
|
|
|
window.googletag.display(self.get("divId"));
|
|
|
|
window.googletag.pubads().refresh([slot.ad]);
|
|
|
|
}
|
2015-10-15 13:58:49 -04:00
|
|
|
});
|
2019-02-22 11:15:40 -05:00
|
|
|
});
|
2019-04-18 14:10:56 -04:00
|
|
|
},
|
2016-05-16 14:26:14 -04:00
|
|
|
|
2019-02-22 12:10:39 -05:00
|
|
|
willRender() {
|
|
|
|
this._super(...arguments);
|
|
|
|
let size = getWidthAndHeight(
|
|
|
|
this.get("placement"),
|
|
|
|
this.siteSettings,
|
|
|
|
this.site.mobileView
|
|
|
|
);
|
|
|
|
this.set("width", size.width);
|
|
|
|
this.set("height", size.height);
|
|
|
|
},
|
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@on("willDestroyElement")
|
|
|
|
cleanup() {
|
2018-10-22 14:49:32 -04:00
|
|
|
destroySlot(this.get("divId"));
|
2019-04-18 14:10:56 -04:00
|
|
|
}
|
2015-07-24 00:01:47 -04:00
|
|
|
});
|