refactor google-dfp-ad component to reduce repetition

This commit is contained in:
Neil Lalonde 2019-02-22 11:15:40 -05:00
parent ccad3ca226
commit 9a695cc67d
1 changed files with 93 additions and 208 deletions

View File

@ -54,206 +54,92 @@ function custom_targeting(key_array, value_array, location) {
} }
} }
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",
sizes: "dfp_topic_above_suggested_ad_sizes",
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"
}
};
function defineSlot(divId, placement, settings, isMobile) { function defineSlot(divId, placement, settings, isMobile) {
if (!settings.dfp_publisher_id) { if (!settings.dfp_publisher_id) {
return; return;
} }
var ad, width, height; let ad, width, height, config;
if (ads[divId]) { if (ads[divId]) {
return ads[divId]; return ads[divId];
} }
if ( if (isMobile) {
placement === "topic-list-top" && config = MOBILE_SETTINGS[placement];
settings.dfp_topic_list_top_code && } else {
settings.dfp_topic_list_top_ad_sizes config = DESKTOP_SETTINGS[placement];
) {
if (isMobile) {
width = parseInt(
splitWidthInt(settings.dfp_mobile_topic_list_top_ad_sizes)
);
height = parseInt(
splitHeightInt(settings.dfp_mobile_topic_list_top_ad_sizes)
);
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_mobile_topic_list_top_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_key_code),
keyParse(settings.dfp_target_topic_list_top_value_code),
ad
);
} else {
width = parseInt(splitWidthInt(settings.dfp_topic_list_top_ad_sizes));
height = parseInt(splitHeightInt(settings.dfp_topic_list_top_ad_sizes));
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_topic_list_top_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_topic_list_top_key_code),
keyParse(settings.dfp_target_topic_list_top_value_code),
ad
);
}
} else if (
placement === "topic-above-post-stream" &&
settings.dfp_topic_above_post_stream_code &&
settings.dfp_topic_above_post_stream_ad_sizes
) {
if (isMobile) {
width = parseInt(
splitWidthInt(settings.dfp_mobile_topic_above_post_stream_ad_sizes)
);
height = parseInt(
splitHeightInt(settings.dfp_mobile_topic_above_post_stream_ad_sizes)
);
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_mobile_topic_above_post_stream_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_topic_above_post_stream_key_code),
keyParse(settings.dfp_target_topic_above_post_stream_value_code),
ad
);
} else {
width = parseInt(
splitWidthInt(settings.dfp_topic_above_post_stream_ad_sizes)
);
height = parseInt(
splitHeightInt(settings.dfp_topic_above_post_stream_ad_sizes)
);
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_topic_above_post_stream_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_topic_above_post_stream_key_code),
keyParse(settings.dfp_target_topic_above_post_stream_value_code),
ad
);
}
} else if (
placement === "topic-above-suggested" &&
settings.dfp_topic_above_suggested_code &&
settings.dfp_topic_above_suggested_ad_sizes
) {
if (isMobile) {
width = parseInt(
splitWidthInt(settings.dfp_mobile_topic_above_suggested_ad_sizes)
);
height = parseInt(
splitHeightInt(settings.dfp_mobile_topic_above_suggested_ad_sizes)
);
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_mobile_topic_above_suggested_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_topic_above_suggested_key_code),
keyParse(settings.dfp_target_topic_above_suggested_value_code),
ad
);
} else {
width = parseInt(
splitWidthInt(settings.dfp_topic_above_suggested_ad_sizes)
);
height = parseInt(
splitHeightInt(settings.dfp_topic_above_suggested_ad_sizes)
);
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_topic_above_suggested_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_topic_above_suggested_key_code),
keyParse(settings.dfp_target_topic_above_suggested_value_code),
ad
);
}
} else if (
placement === "post-bottom" &&
settings.dfp_post_bottom_code &&
settings.dfp_post_bottom_ad_sizes
) {
if (isMobile) {
width = parseInt(splitWidthInt(settings.dfp_mobile_post_bottom_ad_sizes));
height = parseInt(
splitHeightInt(settings.dfp_mobile_post_bottom_ad_sizes)
);
ad = window.googletag
.defineSlot(
"/" +
settings.dfp_publisher_id +
"/" +
settings.dfp_mobile_post_bottom_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_post_bottom_key_code),
keyParse(settings.dfp_target_post_bottom_value_code),
ad
);
} else {
width = parseInt(splitWidthInt(settings.dfp_post_bottom_ad_sizes));
height = parseInt(splitHeightInt(settings.dfp_post_bottom_ad_sizes));
ad = window.googletag
.defineSlot(
"/" + settings.dfp_publisher_id + "/" + settings.dfp_post_bottom_code,
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings.dfp_target_post_bottom_key_code),
keyParse(settings.dfp_target_post_bottom_value_code),
ad
);
}
} }
width = parseInt(splitWidthInt(settings[config.sizes]));
height = parseInt(splitHeightInt(settings[config.sizes]));
ad = window.googletag
.defineSlot(
"/" + settings.dfp_publisher_id + "/" + settings[config.code],
[width, height],
divId
)
.addService(window.googletag.pubads());
custom_targeting(
keyParse(settings[config.targeting_keys]),
keyParse(settings[config.targeting_values]),
ad
);
if (ad) { if (ad) {
ads[divId] = { ad: ad, width: width, height: height }; ads[divId] = { ad: ad, width: width, height: height };
return ads[divId]; return ads[divId];
@ -372,28 +258,27 @@ export default Ember.Component.extend({
} }
var self = this; var self = this;
loadGoogle(this.siteSettings) loadGoogle(this.siteSettings).then(function() {
.then(function() { self.set("loadedGoogletag", true);
self.set("loadedGoogletag", true); window.googletag.cmd.push(function() {
window.googletag.cmd.push(function() { let slot = defineSlot(
let slot = defineSlot( self.get("divId"),
self.get("divId"), self.get("placement"),
self.get("placement"), self.siteSettings,
self.siteSettings, self.site.mobileView
self.site.mobileView );
if (slot && slot.ad) {
slot.ad.setTargeting(
"discourse-category",
self.get("category") ? self.get("category") : "0"
); );
if (slot && slot.ad) { self.set("width", slot.width);
slot.ad.setTargeting( self.set("height", slot.height);
"discourse-category", window.googletag.display(self.get("divId"));
self.get("category") ? self.get("category") : "0" window.googletag.pubads().refresh([slot.ad]);
); }
self.set("width", slot.width);
self.set("height", slot.height);
window.googletag.display(self.get("divId"));
window.googletag.pubads().refresh([slot.ad]);
}
});
}); });
});
}.on("didInsertElement"), }.on("didInsertElement"),
cleanup: function() { cleanup: function() {