2015-07-24 00:01:47 -04:00
|
|
|
import loadScript from 'discourse/lib/load-script';
|
|
|
|
|
2015-08-10 00:19:51 -04:00
|
|
|
var const_width = '';
|
|
|
|
var const_height = '';
|
2015-08-31 23:44:56 -04:00
|
|
|
var const_mobile_width = 320;
|
|
|
|
var const_mobile_height = 50;
|
2015-08-31 21:27:31 -04:00
|
|
|
var currentUser = Discourse.User.current();
|
2015-07-24 00:01:47 -04:00
|
|
|
|
|
|
|
var _loaded = false,
|
2015-10-15 13:58:49 -04:00
|
|
|
_promise = null,
|
|
|
|
ads = {};
|
2015-07-24 00:01:47 -04:00
|
|
|
|
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();
|
|
|
|
}
|
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) {
|
|
|
|
var final = value.replace(/ /g,'');
|
|
|
|
final = final.replace(/['"]+/g, '');
|
|
|
|
final = final.split(',');
|
|
|
|
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;
|
|
|
|
key = key.replace(/['"]+/g, '');
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function defineSlot(placement, settings) {
|
|
|
|
|
|
|
|
if (ads[placement]) {
|
|
|
|
return ads[placement];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placement === "topic-list-top" && settings.dfp_topic_list_top_code && settings.dfp_topic_list_top_ad_sizes) {
|
|
|
|
const_width = parseInt(splitWidthInt(settings.dfp_topic_list_top_ad_sizes));
|
|
|
|
const_height = parseInt(splitHeightInt(settings.dfp_topic_list_top_ad_sizes));
|
|
|
|
if (Discourse.Mobile.mobileView) {
|
2016-02-22 12:11:29 -05:00
|
|
|
var topic_list_top_mobile = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_mobile_topic_list_top_code, [320,50], 'div-gpt-ad-topic-list-top').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['topic-list-top'] = topic_list_top_mobile;
|
|
|
|
custom_targeting((keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_key_code)), (keyParse(settings.dfp_target_topic_list_top_value_code)), topic_list_top_mobile);
|
|
|
|
} else {
|
2016-02-22 12:11:29 -05:00
|
|
|
var topic_list_top = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_list_top_code, [parseInt(splitWidthInt(settings.dfp_topic_list_top_ad_sizes)), parseInt(splitHeightInt(settings.dfp_topic_list_top_ad_sizes))], 'div-gpt-ad-topic-list-top').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['topic-list-top'] = topic_list_top;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_topic_list_top_key_code)), (keyParse(settings.dfp_target_topic_list_top_value_code)), topic_list_top);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ads['topic-list-top'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placement === "topic-above-post-stream" && settings.dfp_topic_above_post_stream_code && settings.dfp_topic_above_post_stream_ad_sizes) {
|
|
|
|
const_width = parseInt(splitWidthInt(settings.dfp_topic_above_post_stream_ad_sizes));
|
|
|
|
const_height = parseInt(splitHeightInt(settings.dfp_topic_above_post_stream_ad_sizes));
|
|
|
|
if (Discourse.Mobile.mobileView) {
|
2016-02-22 12:11:29 -05:00
|
|
|
var topic_above_post_stream_mobile = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_mobile_topic_above_post_stream_code, [320,50], 'div-gpt-ad-topic-above-post-stream').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['topic-above-post-stream'] = topic_above_post_stream_mobile;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_topic_above_post_stream_key_code)), (keyParse(settings.dfp_target_topic_above_post_stream_value_code)), topic_above_post_stream_mobile);
|
|
|
|
}
|
|
|
|
else {
|
2016-02-22 12:11:29 -05:00
|
|
|
var topic_above_post_stream = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_above_post_stream_code, [parseInt(splitWidthInt(settings.dfp_topic_above_post_stream_ad_sizes)), parseInt(splitHeightInt(settings.dfp_topic_above_post_stream_ad_sizes))], 'div-gpt-ad-topic-above-post-stream').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['topic-above-post-stream'] = topic_above_post_stream;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_topic_above_post_stream_key_code)), (keyParse(settings.dfp_target_topic_above_post_stream_value_code)), topic_above_post_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ads['topic-above-post-stream'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placement === "topic-above-suggested" && settings.dfp_topic_above_suggested_code && settings.dfp_topic_above_suggested_ad_sizes) {
|
|
|
|
const_width = parseInt(splitWidthInt(settings.dfp_topic_above_suggested_ad_sizes));
|
|
|
|
const_height = parseInt(splitHeightInt(settings.dfp_topic_above_suggested_ad_sizes));
|
|
|
|
if (Discourse.Mobile.mobileView) {
|
2016-02-22 12:11:29 -05:00
|
|
|
var topic_above_suggested_mobile = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_mobile_topic_above_suggested_code, [320,50], 'div-gpt-ad-topic-above-suggested').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['topic-above-suggested'] = topic_above_suggested_mobile;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_topic_above_suggested_key_code)), (keyParse(settings.dfp_target_topic_above_suggested_value_code)), topic_above_suggested_mobile);
|
|
|
|
}
|
|
|
|
else {
|
2016-02-22 12:11:29 -05:00
|
|
|
var topic_above_suggested = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_above_suggested_code, [parseInt(splitWidthInt(settings.dfp_topic_above_suggested_ad_sizes)), parseInt(splitHeightInt(settings.dfp_topic_above_suggested_ad_sizes))], 'div-gpt-ad-topic-above-suggested').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['topic-above-suggested'] = topic_above_suggested;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_topic_above_suggested_key_code)), (keyParse(settings.dfp_target_topic_above_suggested_value_code)), topic_above_suggested);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ads['topic-above-suggested'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placement === "post-bottom" && settings.dfp_post_bottom_code && settings.dfp_post_bottom_ad_sizes) {
|
|
|
|
const_width = parseInt(splitWidthInt(settings.dfp_post_bottom_ad_sizes));
|
|
|
|
const_height = parseInt(splitHeightInt(settings.dfp_post_bottom_ad_sizes));
|
|
|
|
if (Discourse.Mobile.mobileView) {
|
2016-02-22 12:11:29 -05:00
|
|
|
var post_bottom_mobile = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_mobile_post_bottom_code, [320,50], 'div-gpt-ad-post-bottom').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['post-bottom'] = post_bottom_mobile;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_post_bottom_key_code)), (keyParse(settings.dfp_target_post_bottom_value_code)), post_bottom_mobile);
|
|
|
|
}
|
|
|
|
else {
|
2016-02-22 12:11:29 -05:00
|
|
|
var post_bottom = window.googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_post_bottom_code, [parseInt(splitWidthInt(settings.dfp_post_bottom_ad_sizes)), parseInt(splitHeightInt(settings.dfp_post_bottom_ad_sizes))], 'div-gpt-ad-post-bottom').addService(window.googletag.pubads());
|
2015-10-15 13:58:49 -04:00
|
|
|
ads['post-bottom'] = post_bottom;
|
|
|
|
custom_targeting((keyParse(settings.dfp_target_post_bottom_key_code)), (keyParse(settings.dfp_target_post_bottom_value_code)), post_bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ads['post-bottom'];
|
|
|
|
}
|
2015-08-30 22:21:43 -04:00
|
|
|
}
|
|
|
|
|
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
|
2016-02-22 12:11:29 -05: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) {
|
|
|
|
console.log('googletag is undefined!');
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ember component - the class is the adblock and css
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
const_width: const_width,
|
|
|
|
const_height: const_height,
|
2015-08-31 23:44:56 -04:00
|
|
|
const_mobile_width: const_mobile_width,
|
|
|
|
const_mobile_height: const_mobile_height,
|
2015-07-24 00:01:47 -04:00
|
|
|
|
|
|
|
classNames: ['google-dfp-ad'],
|
|
|
|
loadedGoogletag: false,
|
2015-10-15 13:58:49 -04:00
|
|
|
refreshOnChange: null,
|
2015-07-24 00:01:47 -04:00
|
|
|
|
|
|
|
// Part of the divID of the div part of the GPT
|
|
|
|
divId: function() {
|
|
|
|
return "div-gpt-ad-" + this.get('placement');
|
|
|
|
}.property('placement'),
|
|
|
|
|
2015-08-18 21:44:14 -04:00
|
|
|
adWrapperStyle: function() {
|
|
|
|
return `width: ${this.get('const_width')}px; height: ${this.get('const_height')}px;`.htmlSafe();
|
|
|
|
}.property('const_width', 'const_height'),
|
|
|
|
|
2015-08-19 19:34:52 -04:00
|
|
|
adWrapperStyleMobile: function() {
|
2015-08-31 23:44:56 -04:00
|
|
|
return `width: ${this.get('const_mobile_width')}px; height: ${this.get('const_mobile_height')}px;`.htmlSafe();
|
|
|
|
}.property('const_mobile_width', 'const_mobile_height'),
|
2015-08-19 19:34:52 -04:00
|
|
|
|
2015-10-28 19:51:53 -04:00
|
|
|
adTitleStyleMobile: function() {
|
|
|
|
return `width: ${this.get('const_mobile_width')}px;`.htmlSafe();
|
|
|
|
}.property('const_mobile_width'),
|
|
|
|
|
2015-09-01 23:50:47 -04:00
|
|
|
checkTrustLevels: function() {
|
|
|
|
return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level));
|
|
|
|
}.property('trust_level'),
|
2015-08-19 19:34:52 -04:00
|
|
|
|
2015-10-15 13:58:49 -04:00
|
|
|
refreshAd: function() {
|
|
|
|
var ad = ads[this.get('placement')];
|
|
|
|
if (!ad) { return; }
|
|
|
|
|
2015-10-27 16:42:36 -04:00
|
|
|
var self = this;
|
|
|
|
|
2015-10-27 17:44:03 -04:00
|
|
|
if (this.get('loadedGoogletag') && this.get('refreshOnChange')) {
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.cmd.push(function() {
|
2015-10-28 12:20:06 -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
|
|
|
});
|
|
|
|
}
|
|
|
|
}.observes('refreshOnChange'),
|
|
|
|
|
2015-07-24 00:01:47 -04:00
|
|
|
_initGoogleDFP: function() {
|
|
|
|
var self = this;
|
|
|
|
loadGoogle(this.siteSettings).then(function() {
|
|
|
|
self.set('loadedGoogletag', true);
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.cmd.push(function() {
|
2015-10-15 13:58:49 -04:00
|
|
|
var ad = defineSlot(self.get('placement'), self.siteSettings);
|
|
|
|
if (ad) {
|
2015-10-28 12:20:06 -04:00
|
|
|
ad.setTargeting('discourse-category', self.get('category') ? self.get('category') : '0');
|
2016-02-22 12:11:29 -05:00
|
|
|
window.googletag.display(self.get('divId'));
|
|
|
|
window.googletag.pubads().refresh([ad]);
|
2015-10-15 13:58:49 -04:00
|
|
|
}
|
|
|
|
});
|
2015-07-24 00:01:47 -04:00
|
|
|
});
|
|
|
|
}.on('didInsertElement')
|
|
|
|
});
|