2015-07-24 14:01:47 +10:00
import loadScript from 'discourse/lib/load-script' ;
2015-08-25 15:53:22 +10:00
//import PageTracker from 'discourse/lib/page-tracker';
2015-07-24 14:01:47 +10:00
2015-08-10 14:19:51 +10:00
var const_width = '' ;
var const_height = '' ;
2015-07-24 14:01:47 +10:00
var _loaded = false ,
_promise = null ;
2015-08-10 14:04:21 +10: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-31 16:10:26 +10:00
/*
2015-08-31 12:21:43 +10:00
// Coaches Note!
// Background: We want to call on google.setTargeting using googletag.bar so that we can can take in inputs from users (key and value) for custom targeting.
// Look at 26 - 55 which should call on googletag.setTargeting.
// Error is occuring on link 49 which is returning "Uncaught ReferenceError: setTargeting is not defined"
2015-08-26 15:35:30 +10: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-31 12:21:43 +10: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");
return key;
}
// This sets the key and value for custom targeting
2015-08-31 13:49:09 +10:00
var Foo = function(key, value, googletag) {
2015-08-31 12:21:43 +10:00
this.locationKey = key;
this.locationValue = value;
2015-08-31 13:49:09 +10:00
this.googletag = googletag;
2015-08-31 12:21:43 +10:00
}
// setTargeting is not defined. We want to return as a method - PROBLEM 1
Foo.prototype.bar = function() {
2015-08-31 14:44:47 +10:00
return this.googletag.setTargeting(this.locationKey, this.locationValue);
2015-08-31 12:21:43 +10:00
}
// This should call googletag.setTargeting(key for that location, value for that location)
2015-08-31 14:44:47 +10:00
function custom_targeting(key_array, value_array) {
2015-08-31 15:25:42 +10:00
for (var i = 0; i < key_array.length; i++) {
var wordValue = valueParse(value_array[i]);
2015-08-31 14:44:47 +10:00
var f = new Foo(key_array[i], wordValue, googletag);
f.bar();
2015-08-31 15:25:42 +10:00
console.log("works!");
2015-08-31 14:44:47 +10:00
}
}
2015-08-31 12:21:43 +10:00
// END of Coaches Note
// splitting values
/*var custom_values = [];
var word = keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_value_code);
var wordValue;
for (var i = 0; i < word.length; i++) {
wordValue = valueParse(word[i]);
custom_values.push(wordValue);
}*/
2015-08-25 15:53:22 +10:00
//PageTracker.current().on('change', function(url) {
2015-07-24 14:01:47 +10:00
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!' );
}
googletag . cmd . push ( function () {
2015-08-10 14:04:21 +10:00
if ( settings . dfp_topic_list_top_code && ! settings . dfp_show_topic_list_top && settings . topic_list_top_ad_sizes ) {
2015-08-10 14:19:51 +10:00
const_width = parseInt ( splitWidthInt ( settings . topic_list_top_ad_sizes ));
const_height = parseInt ( splitHeightInt ( settings . topic_list_top_ad_sizes ));
2015-08-20 10:58:31 +10:00
if ( Discourse . Mobile . mobileView ) {
googletag . defineSlot ( settings . dfp_topic_list_top_code , [ 320 , 50 ], 'div-gpt-ad-topic-list-top' ). addService ( googletag . pubads ());
}
else {
2015-08-26 12:04:03 +10:00
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' )
2015-08-31 16:10:26 +10:00
//custom_targeting((keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_key_code)), (keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_value_code)))
. setTargeting ( 'gender' , 'female' )
. addService ( googletag . pubads ());
2015-08-24 12:36:40 +10:00
}
2015-07-24 14:01:47 +10:00
}
2015-08-10 14:04:21 +10: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 14:19:51 +10: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-20 10:58:31 +10:00
if ( Discourse . Mobile . mobileView ) {
googletag . defineSlot ( settings . dfp_topic_above_post_stream_code , [ 320 , 50 ], 'div-gpt-ad-topic-above-post-stream' ). addService ( googletag . pubads ());
}
else {
2015-08-28 12:21:40 +10: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' )
2015-08-31 16:10:26 +10:00
//.setTargeting(settings.dfp_target_topic_above_post_stream_key_code, valueParse(settings.dfp_target_topic_above_post_stream_value_code))
2015-08-28 12:21:40 +10:00
// This hardcoded code works: .setTargeting('category', ["clothes", "handbags", "makeup"])
. addService ( googletag . pubads ());
2015-08-24 12:36:40 +10:00
}
2015-07-24 14:01:47 +10:00
}
2015-08-10 14:04:21 +10:00
if ( settings . dfp_topic_above_suggested_code && ! settings . dfp_show_topic_above_suggested && settings . topic_above_suggested_ad_sizes ) {
2015-08-10 14:19:51 +10:00
const_width = parseInt ( splitWidthInt ( settings . topic_above_suggested_ad_sizes ));
const_height = parseInt ( splitHeightInt ( settings . topic_above_suggested_ad_sizes ));
2015-08-20 10:58:31 +10:00
if ( Discourse . Mobile . mobileView ) {
googletag . defineSlot ( settings . dfp_topic_above_suggested_code , [ 320 , 50 ], 'div-gpt-ad-topic-above-suggested' ). addService ( googletag . pubads ());
}
else {
2015-08-28 12:21:40 +10: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' )
2015-08-28 15:25:11 +10:00
2015-08-31 16:10:26 +10:00
//googletag.setTargeting(settings.dfp_target_topic_above_suggested_key_code, valueParse(settings.dfp_target_topic_above_suggested_value_code))
2015-08-28 12:21:40 +10:00
// This hardcoded code works: .setTargeting('category', ["clothes", "handbags", "makeup"])
2015-08-28 15:25:11 +10:00
googletag . addService ( googletag . pubads ());
2015-08-24 12:36:40 +10:00
}
2015-07-24 14:01:47 +10:00
}
2015-08-10 14:04:21 +10:00
if ( settings . dfp_post_bottom_code && ! settings . dfp_show_post_bottom && settings . post_bottom_ad_sizes ) {
2015-08-10 14:19:51 +10:00
const_width = parseInt ( splitWidthInt ( settings . post_bottom_ad_sizes ));
const_height = parseInt ( splitHeightInt ( settings . post_bottom_ad_sizes ));
2015-08-20 10:58:31 +10:00
if ( Discourse . Mobile . mobileView ) {
googletag . defineSlot ( settings . dfp_post_bottom_code , [ 320 , 50 ], 'div-gpt-ad-post-bottom' ). addService ( googletag . pubads ());
}
else {
2015-08-28 12:21:40 +10: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' )
2015-08-31 16:10:26 +10:00
//.setTargeting(settings.dfp_target_post_bottom_key_code, valueParse(settings.dfp_target_post_bottom_value_code))
2015-08-28 12:21:40 +10:00
// This hardcoded code works: .setTargeting('category', ["clothes", "handbags", "makeup"])
. addService ( googletag . pubads ());
2015-08-24 12:36:40 +10:00
}
2015-07-30 11:05:33 +10:00
}
2015-08-19 10:45:03 +10:00
2015-08-20 08:23:55 +10:00
// Page Level custom targeting goes here - needs an input section and also ad tags on the relevant pages
2015-07-24 14:01:47 +10:00
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' ),
2015-08-19 11:44:14 +10:00
adWrapperStyle : function () {
return `width: ${ this . get ( 'const_width' ) } px; height: ${ this . get ( 'const_height' ) } px;` . htmlSafe ();
}. property ( 'const_width' , 'const_height' ),
2015-08-20 09:34:52 +10:00
adWrapperStyleMobile : function () {
return `width: 320px; height: 50px;` . htmlSafe ();
},
2015-07-24 14:01:47 +10:00
_initGoogleDFP : function () {
var self = this ;
loadGoogle ( this . siteSettings ). then ( function () {
self . set ( 'loadedGoogletag' , true );
});
}. on ( 'didInsertElement' )
});