Skip to content

Instantly share code, notes, and snippets.

@7LayersDesign
Last active December 25, 2015 19:49
Show Gist options
  • Save 7LayersDesign/7030369 to your computer and use it in GitHub Desktop.
Save 7LayersDesign/7030369 to your computer and use it in GitHub Desktop.
Only init SF test if ?safeframe is included in url. Including showlog will show the log also.
<script type="text/javascript">
$('#leaderboard').append('<div id="tgtSFLB"></div>');
</script>
<script type="text/javascript">
/**
*
* SafeFrame Initialization Code
* @author Andy Hutchins <ahutchins@scrippsnetworks.com>
* @module SNI.Ads.SafeFrame
* @version 0.0.3
*
**/
// instantiate Ads object/namespace. This should only be required for local testing. In prod and stage, SNI.Ads should be created already by the page.
if (typeof(SNI.Ads) == 'undefined') {
SNI.Ads = {
// should be adsremote.scrippsnetworks.com for production and devadsremote.scrippsnetworks.com for development/staging
_adServerHostname: SNI.Config.adServerUrl
};
}
SNI.Ads.SafeFrame = {
/**
* Call when message recieved from external API
*/
_onPosMsgCallback: function(id, msg, data) {
var _self = SNI.Ads.SafeFrame;
switch (msg) {
case 'geom-update':
var perc = Math.round(data.self.iv * 100);
// _self._log("In View: (" + id + ") " + perc + "%");
if (id === 'SFBB') {
_self._viewLog("bb", perc);
}
if (id === 'SFLB') {
_self._viewLog("lb", perc);
}
_self._clog(perc);
break;
default:
_self._log(id);
break;
} // end switch
},
/**
* Internal Method for setting up in page console.
*/
_logInit: function() {
if (!this.debug) return;
$('body').prepend('<div id="sf-log"><h2>SafeFrame Log</h2><h3>Viewability</h3><ul><li id="bb">Bigbox: <span>0</span></li><li id="lb">Leaderboard: <span>0</span></li></ul></div>');
var styles = [
"<style>",
"#sf-log{position:fixed;z-index:100000;top:0;left:0;background:#fff;padding:14px;border-bottom:2px solid #000;border-right:2px solid #000;min-width:200px;line-height:1.2}",
"#sf-log h2{color:#c12511;font-size:24px;}",
"#sf-log li{font-size:16px;color:#333;}",
"#sf-log li span{float: right;}",
"</style>"
].join('\n');
$('head').append(styles);
},
/**
* Internal method for writing logs to on page console.
* @param {String} msg The message
*/
_log: function(msg) {
this.logCount++;
if (!this.debug) return;
// Add another log item
$('#sf-log ul').append("<li>" + this.logCount + ": " + msg + "</li>");
},
/**
* Record the SafeFrame viewable status
* @param {String} adtype The ad type
* @param {String} value The percentage of ad in view of host page
*/
_viewLog: function(adtype, value) {
if (value <= 100) {
$('#' + adtype).find('span').text(value + '%');
}
},
/**
* Log using browser console.
* @param {Object} arg What to log
*/
_clog: function(obj) {
// make sure browser provides a console
if( typeof(console) === "object" ){
console.log(obj);
}
},
/*======================================
= Public Methods =
======================================*/
/**
* Initialize SafeFrame containers on page.
* @param {boolean} debug If set, the dev adserver will be used.
* @return {[type]} [description]
*/
init: function() {
var _self = this;
var qs = window.location.search.substr(1).split("&");
// Cancel if safeframe flag not set.
if( qs.indexOf("safeframe") === -1 ) {
return false;
}
// Determine if log should be shown
this.debug = (qs.indexOf("showlog") > -1 ) ? 1 : 0;
this.logCount = 0;
if (this.debug) {
// Use dev adserver in debug mode
SNI.Ads._adServerHostname = "devadsremote.scrippsnetworks.com";
_self._logInit();
console.log("DEBUG MODE");
}
// SafeFrame configuration
var conf = new $sf.host.Config({
renderFile: "http://adimages.scrippsnetworks.com/ad_ops/safeframe/1-0-1/html/r.html",
onPosMsg: _self._onPosMsgCallback,
positions: {
"SFLB": {
id: "SFLB",
dest: "tgtSFLB",
src: "http://adsremote.scrippsnetworks.com/html.js/adtype=LEADERBOARD&Pagepos=1&site=HGTV",
w: 728,
h: 90
}
}
});
}
} // END SNI.Ads.SafeFrame
</script>
<script type="text/javascript">
// Init safeframe
SNI.Ads.SafeFrame.init();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment