2022-03-06 13:52:19 -05:00
|
|
|
import { alias } from "@ember/object/computed";
|
2023-11-07 12:07:33 -05:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2024-11-29 05:11:21 -05:00
|
|
|
import { classNameBindings, classNames } from "@ember-decorators/component";
|
2022-03-06 13:52:19 -05:00
|
|
|
import RSVP from "rsvp";
|
2023-11-07 12:07:33 -05:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2022-03-06 13:52:19 -05:00
|
|
|
import { isTesting } from "discourse-common/config/environment";
|
2023-11-07 12:07:33 -05:00
|
|
|
import discourseComputed, { on } from "discourse-common/utils/decorators";
|
|
|
|
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2019-06-10 15:43:48 -04:00
|
|
|
let _loaded = false,
|
2018-10-22 14:49:32 -04:00
|
|
|
_promise = null,
|
2019-06-10 15:43:48 -04:00
|
|
|
ads = {},
|
2019-07-08 15:10:25 -04:00
|
|
|
nextSlotNum = 1,
|
|
|
|
renderCounts = {};
|
2019-06-10 15:43:48 -04:00
|
|
|
|
|
|
|
function getNextSlotNum() {
|
|
|
|
return nextSlotNum++;
|
|
|
|
}
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2015-08-10 00:04:21 -04:00
|
|
|
function splitWidthInt(value) {
|
2021-01-27 04:36:36 -05:00
|
|
|
let str = value.substring(0, 3);
|
2018-10-22 14:49:32 -04:00
|
|
|
return str.trim();
|
2015-08-10 00:04:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function splitHeightInt(value) {
|
2021-01-27 04:36:36 -05:00
|
|
|
let str = value.substring(4, 7);
|
2018-10-22 14:49:32 -04:00
|
|
|
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) {
|
2019-06-10 15:43:48 -04:00
|
|
|
let final = value.replace(/ /g, "");
|
2018-10-22 14:49:32 -04:00
|
|
|
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) {
|
2019-06-10 15:43:48 -04:00
|
|
|
let 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 should call adslot.setTargeting(key for that location, value for that location)
|
2019-06-10 15:43:48 -04:00
|
|
|
function custom_targeting(key_array, value_array, adSlot) {
|
2021-01-27 04:36:36 -05:00
|
|
|
for (let i = 0; i < key_array.length; i++) {
|
2019-06-10 15:43:48 -04:00
|
|
|
if (key_array[i]) {
|
2019-06-14 13:24:55 -04:00
|
|
|
adSlot.setTargeting(key_array[i], valueParse(value_array[i]));
|
2019-06-10 15:43:48 -04:00
|
|
|
}
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_topic_list_top_value_code",
|
2019-02-22 11:15:40 -05:00
|
|
|
},
|
|
|
|
"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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_topic_above_post_stream_value_code",
|
2019-02-22 11:15:40 -05:00
|
|
|
},
|
|
|
|
"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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_topic_above_suggested_value_code",
|
2019-02-22 11:15:40 -05:00
|
|
|
},
|
|
|
|
"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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_post_bottom_value_code",
|
|
|
|
},
|
2019-02-22 11:15:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_topic_list_top_value_code",
|
2019-02-22 11:15:40 -05:00
|
|
|
},
|
|
|
|
"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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_topic_above_post_stream_value_code",
|
2019-02-22 11:15:40 -05:00
|
|
|
},
|
|
|
|
"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",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_topic_above_suggested_value_code",
|
2019-02-22 11:15:40 -05:00
|
|
|
},
|
|
|
|
"post-bottom": {
|
|
|
|
code: "dfp_mobile_post_bottom_code",
|
|
|
|
sizes: "dfp_mobile_post_bottom_ad_sizes",
|
|
|
|
targeting_keys: "dfp_target_post_bottom_key_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
targeting_values: "dfp_target_post_bottom_value_code",
|
|
|
|
},
|
2019-02-22 11:15:40 -05:00
|
|
|
};
|
|
|
|
|
2019-02-22 12:10:39 -05:00
|
|
|
function getWidthAndHeight(placement, settings, isMobile) {
|
2019-07-08 15:10:25 -04:00
|
|
|
let config, size;
|
2019-02-22 12:10:39 -05:00
|
|
|
|
|
|
|
if (isMobile) {
|
|
|
|
config = MOBILE_SETTINGS[placement];
|
|
|
|
} else {
|
|
|
|
config = DESKTOP_SETTINGS[placement];
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:10:25 -04:00
|
|
|
if (!renderCounts[placement]) {
|
|
|
|
renderCounts[placement] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sizes = (settings[config.sizes] || "").split("|");
|
|
|
|
|
2019-07-08 15:18:34 -04:00
|
|
|
if (sizes.length === 1) {
|
2019-07-08 15:10:25 -04:00
|
|
|
size = sizes[0];
|
|
|
|
} else {
|
|
|
|
size = sizes[renderCounts[placement] % sizes.length];
|
|
|
|
renderCounts[placement] += 1;
|
|
|
|
}
|
|
|
|
|
2020-01-15 16:52:02 -05:00
|
|
|
if (size === "fluid") {
|
2020-01-15 18:12:27 -05:00
|
|
|
return { width: "fluid", height: "fluid" };
|
2020-01-15 16:52:02 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 17:01:01 -04:00
|
|
|
const sizeObj = {
|
2019-11-18 10:07:55 -05:00
|
|
|
width: parseInt(splitWidthInt(size), 10),
|
2020-09-04 07:24:14 -04:00
|
|
|
height: parseInt(splitHeightInt(size), 10),
|
2019-02-22 12:10:39 -05:00
|
|
|
};
|
2019-09-10 17:01:01 -04:00
|
|
|
|
|
|
|
if (!isNaN(sizeObj.width) && !isNaN(sizeObj.height)) {
|
|
|
|
return sizeObj;
|
|
|
|
}
|
2019-02-22 12:10:39 -05:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:31:25 -04:00
|
|
|
function defineSlot(
|
|
|
|
divId,
|
|
|
|
placement,
|
|
|
|
settings,
|
|
|
|
isMobile,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
categoryTarget
|
|
|
|
) {
|
2016-11-21 15:55:03 -05:00
|
|
|
if (!settings.dfp_publisher_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-21 18:09:46 -04:00
|
|
|
if (ads[divId]) {
|
|
|
|
return ads[divId];
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 11:52:23 -04:00
|
|
|
let ad, config, publisherId;
|
|
|
|
|
2019-02-22 11:15:40 -05:00
|
|
|
if (isMobile) {
|
2019-06-19 11:52:23 -04:00
|
|
|
publisherId = settings.dfp_publisher_id_mobile || settings.dfp_publisher_id;
|
2019-02-22 11:15:40 -05:00
|
|
|
config = MOBILE_SETTINGS[placement];
|
|
|
|
} else {
|
2019-06-19 11:52:23 -04:00
|
|
|
publisherId = settings.dfp_publisher_id;
|
2019-02-22 11:15:40 -05:00
|
|
|
config = DESKTOP_SETTINGS[placement];
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-14 13:24:55 -04:00
|
|
|
ad = window.googletag.defineSlot(
|
2019-06-19 11:52:23 -04:00
|
|
|
"/" + publisherId + "/" + settings[config.code],
|
2019-07-15 15:31:25 -04:00
|
|
|
[width, height],
|
2019-06-14 13:24:55 -04:00
|
|
|
divId
|
|
|
|
);
|
2019-06-10 15:43:48 -04:00
|
|
|
|
2019-02-22 11:15:40 -05:00
|
|
|
custom_targeting(
|
|
|
|
keyParse(settings[config.targeting_keys]),
|
|
|
|
keyParse(settings[config.targeting_values]),
|
|
|
|
ad
|
|
|
|
);
|
|
|
|
|
2019-06-10 15:43:48 -04:00
|
|
|
if (categoryTarget) {
|
|
|
|
ad.setTargeting("discourse-category", categoryTarget);
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
2019-06-10 15:43:48 -04:00
|
|
|
|
|
|
|
ad.addService(window.googletag.pubads());
|
|
|
|
|
2021-11-09 19:58:31 -05:00
|
|
|
ads[divId] = { ad, width, height };
|
2019-06-10 15:43:48 -04:00
|
|
|
return ads[divId];
|
2015-08-30 22:21:43 -04:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:26:14 -04:00
|
|
|
function destroySlot(divId) {
|
|
|
|
if (ads[divId] && window.googletag) {
|
2019-06-10 15:43:48 -04:00
|
|
|
window.googletag.destroySlots([ads[divId].ad]);
|
|
|
|
delete ads[divId];
|
2016-05-16 14:26:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 12:11:29 -05:00
|
|
|
function loadGoogle() {
|
2019-06-10 15:43:48 -04:00
|
|
|
/**
|
|
|
|
* Refer to this article for help:
|
|
|
|
* https://support.google.com/admanager/answer/4578089?hl=en
|
|
|
|
*/
|
|
|
|
|
2015-07-24 00:01:47 -04:00
|
|
|
if (_loaded) {
|
2022-03-06 13:52:19 -05:00
|
|
|
return RSVP.resolve();
|
2015-07-24 00:01:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_promise) {
|
|
|
|
return _promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The boilerplate code
|
2021-01-27 04:36:36 -05:00
|
|
|
let dfpSrc =
|
2018-10-22 14:49:32 -04:00
|
|
|
("https:" === document.location.protocol ? "https:" : "http:") +
|
2020-03-13 14:24:54 -04:00
|
|
|
"//securepubads.g.doubleclick.net/tag/js/gpt.js";
|
2020-09-04 07:24:14 -04:00
|
|
|
_promise = loadScript(dfpSrc, { scriptTag: true }).then(function () {
|
2015-07-24 00:01:47 -04:00
|
|
|
_loaded = true;
|
|
|
|
if (window.googletag === undefined) {
|
2019-06-14 12:10:11 -04:00
|
|
|
// eslint-disable-next-line no-console
|
2018-10-22 14:49:32 -04:00
|
|
|
console.log("googletag is undefined!");
|
2015-07-24 00:01:47 -04:00
|
|
|
}
|
|
|
|
|
2020-09-04 07:24:14 -04:00
|
|
|
window.googletag.cmd.push(function () {
|
2019-06-10 15:43:48 -04:00
|
|
|
// Infinite scroll requires SRA:
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.pubads().enableSingleRequest();
|
2019-06-10 15:43:48 -04:00
|
|
|
|
|
|
|
// we always use refresh() to fetch the ads:
|
|
|
|
window.googletag.pubads().disableInitialLoad();
|
|
|
|
|
2024-02-19 07:35:22 -05:00
|
|
|
// Improve CSP compatibility (https://developers.google.com/publisher-tag/guides/content-security-policy)
|
|
|
|
window.googletag.pubads().setForceSafeFrame(true);
|
|
|
|
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.enableServices();
|
2015-07-24 00:01:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-14 13:24:55 -04:00
|
|
|
window.googletag = window.googletag || { cmd: [] };
|
2019-06-10 15:43:48 -04:00
|
|
|
|
2015-07-24 00:01:47 -04:00
|
|
|
return _promise;
|
|
|
|
}
|
|
|
|
|
2024-11-29 05:11:21 -05:00
|
|
|
@classNameBindings("adUnitClass")
|
|
|
|
@classNames("google-dfp-ad")
|
|
|
|
export default class GoogleDfpAd extends AdComponent {
|
|
|
|
loadedGoogletag = false;
|
|
|
|
lastAdRefresh = null;
|
|
|
|
|
|
|
|
@alias("size.width") width;
|
|
|
|
|
|
|
|
@alias("size.height") height;
|
2019-07-15 15:31:25 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed
|
2019-07-15 15:31:25 -04:00
|
|
|
size() {
|
|
|
|
return getWidthAndHeight(
|
|
|
|
this.get("placement"),
|
|
|
|
this.siteSettings,
|
|
|
|
this.site.mobileView
|
|
|
|
);
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed(
|
2019-06-19 11:52:23 -04:00
|
|
|
"siteSettings.dfp_publisher_id",
|
|
|
|
"siteSettings.dfp_publisher_id_mobile",
|
|
|
|
"site.mobileView"
|
|
|
|
)
|
|
|
|
publisherId(globalId, mobileId, isMobile) {
|
|
|
|
if (isMobile) {
|
|
|
|
return mobileId || globalId;
|
|
|
|
} else {
|
|
|
|
return globalId;
|
|
|
|
}
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2019-06-19 11:52:23 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("placement", "postNumber")
|
2019-04-18 14:10:56 -04:00
|
|
|
divId(placement, postNumber) {
|
2019-06-10 15:43:48 -04:00
|
|
|
let slotNum = getNextSlotNum();
|
2019-04-18 14:10:56 -04:00
|
|
|
if (postNumber) {
|
2019-06-10 15:43:48 -04:00
|
|
|
return `div-gpt-ad-${slotNum}-${placement}-${postNumber}`;
|
2016-04-21 18:09:46 -04:00
|
|
|
} else {
|
2019-06-10 15:43:48 -04:00
|
|
|
return `div-gpt-ad-${slotNum}-${placement}`;
|
2016-04-21 18:09:46 -04:00
|
|
|
}
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2015-07-24 00:01:47 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("placement", "showAd")
|
2019-04-18 14:10:56 -04:00
|
|
|
adUnitClass(placement, showAd) {
|
|
|
|
return showAd ? `dfp-ad-${placement}` : "";
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2016-04-21 14:59:59 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("width", "height")
|
2019-04-18 14:10:56 -04:00
|
|
|
adWrapperStyle(w, h) {
|
2020-01-15 18:12:27 -05:00
|
|
|
if (w !== "fluid") {
|
2022-06-01 12:37:01 -04:00
|
|
|
return htmlSafe(`width: ${w}px; height: ${h}px;`);
|
2020-01-15 16:52:02 -05:00
|
|
|
}
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2015-08-19 19:34:52 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("width")
|
2019-04-18 14:10:56 -04:00
|
|
|
adTitleStyleMobile(w) {
|
2020-01-15 18:12:27 -05:00
|
|
|
if (w !== "fluid") {
|
2022-06-01 12:37:01 -04:00
|
|
|
return htmlSafe(`width: ${w}px;`);
|
2020-01-15 16:52:02 -05:00
|
|
|
}
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2015-10-28 19:51:53 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed(
|
2019-06-19 11:52:23 -04:00
|
|
|
"publisherId",
|
2024-02-08 21:42:40 -05:00
|
|
|
"showDfpAds",
|
2019-06-05 15:52:52 -04:00
|
|
|
"showToGroups",
|
|
|
|
"showAfterPost",
|
2019-09-10 17:01:01 -04:00
|
|
|
"showOnCurrentPage",
|
|
|
|
"size"
|
2019-06-05 15:52:52 -04:00
|
|
|
)
|
2019-06-19 11:52:23 -04:00
|
|
|
showAd(
|
|
|
|
publisherId,
|
2024-02-08 21:42:40 -05:00
|
|
|
showDfpAds,
|
2019-06-19 11:52:23 -04:00
|
|
|
showToGroups,
|
|
|
|
showAfterPost,
|
2019-09-10 17:01:01 -04:00
|
|
|
showOnCurrentPage,
|
|
|
|
size
|
2019-06-19 11:52:23 -04:00
|
|
|
) {
|
2018-10-22 14:49:32 -04:00
|
|
|
return (
|
2019-06-19 11:52:23 -04:00
|
|
|
publisherId &&
|
2024-02-08 21:42:40 -05:00
|
|
|
showDfpAds &&
|
2019-04-18 17:52:59 -04:00
|
|
|
showToGroups &&
|
2019-06-05 15:52:52 -04:00
|
|
|
showAfterPost &&
|
2019-09-10 17:01:01 -04:00
|
|
|
showOnCurrentPage &&
|
|
|
|
size
|
2018-10-22 14:49:32 -04:00
|
|
|
);
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2016-11-21 15:55:03 -05:00
|
|
|
|
2024-01-31 13:04:24 -05:00
|
|
|
@discourseComputed
|
2024-02-08 21:42:40 -05:00
|
|
|
showDfpAds() {
|
2024-01-31 13:04:24 -05:00
|
|
|
if (!this.currentUser) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.currentUser.show_dfp_ads;
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2015-08-19 19:34:52 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("postNumber")
|
2019-04-18 17:52:59 -04:00
|
|
|
showAfterPost(postNumber) {
|
|
|
|
if (!postNumber) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-18 10:07:55 -05:00
|
|
|
return this.isNthPost(parseInt(this.siteSettings.dfp_nth_post_code, 10));
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2019-06-26 11:15:04 -04:00
|
|
|
// 3 second delay between calls to refresh ads in a component.
|
|
|
|
// Ember often calls updated() more than once, and *sometimes*
|
|
|
|
// updated() is called after _initGoogleDFP().
|
|
|
|
shouldRefreshAd() {
|
|
|
|
const lastAdRefresh = this.get("lastAdRefresh");
|
|
|
|
if (!lastAdRefresh) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return new Date() - lastAdRefresh > 3000;
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2019-06-26 11:15:04 -04:00
|
|
|
|
2019-06-05 17:00:47 -04:00
|
|
|
@on("didUpdate")
|
|
|
|
updated() {
|
2023-11-07 16:12:30 -05:00
|
|
|
if (!this.shouldRefreshAd()) {
|
2019-06-05 17:00:47 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let slot = ads[this.get("divId")];
|
2018-10-22 14:49:32 -04:00
|
|
|
if (!(slot && slot.ad)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-15 13:58:49 -04:00
|
|
|
|
2019-06-05 17:00:47 -04:00
|
|
|
let ad = slot.ad,
|
|
|
|
categorySlug = this.get("currentCategorySlug");
|
2015-10-27 16:42:36 -04:00
|
|
|
|
2019-06-05 17:00:47 -04:00
|
|
|
if (this.get("loadedGoogletag")) {
|
2019-06-26 11:15:04 -04:00
|
|
|
this.set("lastAdRefresh", new Date());
|
|
|
|
window.googletag.cmd.push(() => {
|
2019-06-05 17:00:47 -04:00
|
|
|
ad.setTargeting("discourse-category", categorySlug || "0");
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.pubads().refresh([ad]);
|
2015-10-15 13:58:49 -04:00
|
|
|
});
|
|
|
|
}
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2015-10-15 13:58:49 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@on("didInsertElement")
|
|
|
|
_initGoogleDFP() {
|
2022-03-06 13:52:19 -05:00
|
|
|
if (isTesting()) {
|
2020-01-06 12:01:40 -05:00
|
|
|
return; // Don't load external JS during tests
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
if (!this.get("showAd")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-21 15:55:03 -05:00
|
|
|
|
2020-09-23 09:05:23 -04:00
|
|
|
loadGoogle().then(() => {
|
2019-06-26 11:15:04 -04:00
|
|
|
this.set("loadedGoogletag", true);
|
|
|
|
this.set("lastAdRefresh", new Date());
|
2019-07-15 15:31:25 -04:00
|
|
|
|
2019-06-26 11:15:04 -04:00
|
|
|
window.googletag.cmd.push(() => {
|
2019-02-22 11:15:40 -05:00
|
|
|
let slot = defineSlot(
|
2019-06-26 11:15:04 -04:00
|
|
|
this.get("divId"),
|
|
|
|
this.get("placement"),
|
|
|
|
this.siteSettings,
|
|
|
|
this.site.mobileView,
|
2019-07-15 15:31:25 -04:00
|
|
|
this.get("width"),
|
|
|
|
this.get("height"),
|
2019-06-26 11:15:04 -04:00
|
|
|
this.get("currentCategorySlug") || "0"
|
2019-02-22 11:15:40 -05:00
|
|
|
);
|
|
|
|
if (slot && slot.ad) {
|
2019-06-10 15:43:48 -04:00
|
|
|
// Display has to be called before refresh
|
|
|
|
// and after the slot div is in the page.
|
2019-06-26 11:15:04 -04:00
|
|
|
window.googletag.display(this.get("divId"));
|
2019-02-22 11:15:40 -05:00
|
|
|
window.googletag.pubads().refresh([slot.ad]);
|
|
|
|
}
|
2015-10-15 13:58:49 -04:00
|
|
|
});
|
2019-02-22 11:15:40 -05:00
|
|
|
});
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2016-05-16 14:26:14 -04:00
|
|
|
|
2019-02-22 12:10:39 -05:00
|
|
|
willRender() {
|
2024-11-29 05:11:21 -05:00
|
|
|
super.willRender(...arguments);
|
2019-06-10 15:43:48 -04:00
|
|
|
|
|
|
|
if (!this.get("showAd")) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
2019-02-22 12:10:39 -05:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@on("willDestroyElement")
|
|
|
|
cleanup() {
|
2018-10-22 14:49:32 -04:00
|
|
|
destroySlot(this.get("divId"));
|
2024-11-29 05:11:21 -05:00
|
|
|
}
|
|
|
|
}
|