discourse-adplugin/assets/javascripts/discourse/components/google-dfp-ad.js.es6

94 lines
4.5 KiB
Plaintext
Raw Normal View History

import loadScript from 'discourse/lib/load-script';
2015-08-10 00:19:51 -04:00
var const_width = '';
var const_height = '';
var _loaded = false,
_promise = null;
2015-08-10 00:04:21 -04:00
function splitWidthInt(value) {
var str = value.substring(0, 3);
return str.trim();
}
function splitHeightInt(value) {
var str = value.substring(4, 7);
return str.trim();
}
function loadGoogle(settings) {
if (_loaded) {
return Ember.RSVP.resolve();
}
if (_promise) {
return _promise;
}
// The boilerplate code
var dfpSrc = (('https:' == document.location.protocol) ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
_promise = loadScript(dfpSrc, { scriptTag: true }).then(function() {
_loaded = true;
if (window.googletag === undefined) {
console.log('googletag is undefined!');
}
// Define our ad units - extend for mobile view.
2015-08-19 18:23:55 -04:00
// Inventory or slot level custom targeting goes here for each of the defined ad units - needs input and injection.
// For each ad slot, if there is set target, then don't put semicolon in after addService, if so, then add it in to close off function.
// Need to add something to the actual site itself... on the page googletag.pubads().setTargeting(publisher, affiliate1);:
googletag.cmd.push(function() {
2015-08-10 00:04:21 -04:00
if (settings.dfp_topic_list_top_code && !settings.dfp_show_topic_list_top && settings.topic_list_top_ad_sizes) {
2015-08-10 00:19:51 -04:00
const_width = parseInt(splitWidthInt(settings.topic_list_top_ad_sizes));
const_height = parseInt(splitHeightInt(settings.topic_list_top_ad_sizes));
googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_list_top_code, [parseInt(splitWidthInt(settings.topic_list_top_ad_sizes)), parseInt(splitHeightInt(settings.topic_list_top_ad_sizes))], 'div-gpt-ad-topic-list-top').setTargeting('gender', ['female']).addService(googletag.pubads());
}
2015-08-10 00:04:21 -04:00
if (settings.dfp_topic_above_post_stream_code && !settings.dfp_show_topic_above_post_stream && settings.topic_above_post_stream_ad_sizes) {
2015-08-10 00:19:51 -04:00
const_width = parseInt(splitWidthInt(settings.topic_above_post_stream_ad_sizes));
const_height = parseInt(splitHeightInt(settings.topic_above_post_stream_ad_sizes));
2015-08-19 20:19:29 -04:00
googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_above_post_stream_code, [parseInt(splitWidthInt(settings.topic_above_post_stream_ad_sizes)), parseInt(splitHeightInt(settings.topic_above_post_stream_ad_sizes))], 'div-gpt-ad-topic-above-post-stream').addService(googletag.pubads());
}
2015-08-10 00:04:21 -04:00
if (settings.dfp_topic_above_suggested_code && !settings.dfp_show_topic_above_suggested && settings.topic_above_suggested_ad_sizes) {
2015-08-10 00:19:51 -04:00
const_width = parseInt(splitWidthInt(settings.topic_above_suggested_ad_sizes));
const_height = parseInt(splitHeightInt(settings.topic_above_suggested_ad_sizes));
2015-08-19 20:19:29 -04:00
googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_above_suggested_code, [parseInt(splitWidthInt(settings.topic_above_suggested_ad_sizes)), parseInt(splitHeightInt(settings.topic_above_suggested_ad_sizes))], 'div-gpt-ad-topic-above-suggested').addService(googletag.pubads());
}
2015-08-10 00:04:21 -04:00
if (settings.dfp_post_bottom_code && !settings.dfp_show_post_bottom && settings.post_bottom_ad_sizes) {
2015-08-10 00:19:51 -04:00
const_width = parseInt(splitWidthInt(settings.post_bottom_ad_sizes));
const_height = parseInt(splitHeightInt(settings.post_bottom_ad_sizes));
2015-08-19 20:19:29 -04:00
googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_post_bottom_code, [parseInt(splitWidthInt(settings.post_bottom_ad_sizes)), parseInt(splitHeightInt(settings.post_bottom_ad_sizes))], 'div-gpt-ad-post-bottom').addService(googletag.pubads());
2015-07-29 21:05:33 -04:00
}
2015-08-19 18:23:55 -04:00
// Page Level custom targeting goes here - needs an input section and also ad tags on the relevant pages
// googletag.pubads().setTargeting("gender","female");
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
});
return _promise;
}
// Ember component - the class is the adblock and css
export default Ember.Component.extend({
const_width: const_width,
const_height: const_height,
classNames: ['google-dfp-ad'],
loadedGoogletag: false,
// Part of the divID of the div part of the GPT
divId: function() {
return "div-gpt-ad-" + this.get('placement');
}.property('placement'),
_initGoogleDFP: function() {
var self = this;
loadGoogle(this.siteSettings).then(function() {
self.set('loadedGoogletag', true);
});
}.on('didInsertElement')
});