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,205 +54,91 @@ 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 (
placement === "topic-list-top" &&
settings.dfp_topic_list_top_code &&
settings.dfp_topic_list_top_ad_sizes
) {
if (isMobile) { if (isMobile) {
width = parseInt( config = MOBILE_SETTINGS[placement];
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 { } else {
width = parseInt(splitWidthInt(settings.dfp_topic_list_top_ad_sizes)); config = DESKTOP_SETTINGS[placement];
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" && width = parseInt(splitWidthInt(settings[config.sizes]));
settings.dfp_topic_above_post_stream_code && height = parseInt(splitHeightInt(settings[config.sizes]));
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 ad = window.googletag
.defineSlot( .defineSlot(
"/" + "/" + settings.dfp_publisher_id + "/" + settings[config.code],
settings.dfp_publisher_id +
"/" +
settings.dfp_mobile_topic_above_post_stream_code,
[width, height], [width, height],
divId divId
) )
.addService(window.googletag.pubads()); .addService(window.googletag.pubads());
custom_targeting( custom_targeting(
keyParse(settings.dfp_target_topic_above_post_stream_key_code), keyParse(settings[config.targeting_keys]),
keyParse(settings.dfp_target_topic_above_post_stream_value_code), keyParse(settings[config.targeting_values]),
ad 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
);
}
}
if (ad) { if (ad) {
ads[divId] = { ad: ad, width: width, height: height }; ads[divId] = { ad: ad, width: width, height: height };
@ -372,8 +258,7 @@ 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(