Skip to content

Instantly share code, notes, and snippets.

@rinatkhaziev
Created April 26, 2012 00:53
Show Gist options
  • Save rinatkhaziev/2494899 to your computer and use it in GitHub Desktop.
Save rinatkhaziev/2494899 to your computer and use it in GitHub Desktop.
DFP Small Business
<?php
/**
* Additional setup for our freshly baked Ad Code Manager v0.2
* Not tested with v0.1
*
* Quick config for DFP JS API
*
*/
// Add additional output tokens
add_filter('acm_output_tokens', 'sp_acm_output_tokens', 999, 3 );
/**
* We don't need that much of a configuration for JS DFP Tags
*
*/
function sp_acm_output_tokens( $output_tokens, $tag_id, $code_to_display ) {
$output_tokens['%tag_id%'] = $tag_id;
// IMPORTANT: Your Doubleclick For Publishers id
// https://www.google.com/dfp/id
$output_tokens['%site_name%'] = 6287139;
$output_tokens['%test%'] = isset( $_GET['test'] ) && $_GET['test'] == 'on' ? 'on' : '';
return $output_tokens;
}
add_filter( 'acm_list_table_columns', 'sp_acm_list_table_columns' );
function sp_acm_list_table_columns($columns) {
$columns = array(
'id' => __( 'ID', 'ad-code-manager' ),
'name' => __( 'Name', 'ad-code-manager' ),
'priority' => __( 'Priority', 'ad-code-manager' ),
'conditionals' => __( 'Conditionals', 'ad-code-manager' ),
);
return $columns;
}
add_filter( 'acm_provider_columns', 'sp_acm_provider_columns' );
function sp_acm_provider_columns( $columns ) {
return
array(
'name' => __( 'Name', 'ad-code-manager' ),
);
}
// 'tag' should be equal to DFP Ad Unit name
// in my example it's "sp_leaderboard_atf"
// but if you stick to DFP naming convention
// you could get something like "ABCProductsUnlimited_HelpCenter_Tutorials_BTF_footer_728x90"
// Seriously, what?
add_filter( 'acm_ad_tag_ids', 'sp_ad_tags_ids' );
function sp_ad_tags_ids( $ad_tag_ids ) {
return array(
array(
'tag' => 'sp_leaderboard_atf',
'url_vars' => array(
'sz' => '728x90',
'fold' => 'atf',
'width' => '728',
'height' => '90',
)
),
array(
'tag' => 'sp_mrec_atf',
'url_vars' => array(
'sz' => '300x250',
'fold' => 'atf',
'width' => '300',
'height' => '250',
)
),
array(
'tag' => 'sp_mrec_btf',
'url_vars' => array(
'sz' => '300x250',
'fold' => 'btf',
'width' => '300',
'height' => '250',
)
),
array(
'tag' => 'sp_widesky_atf',
'url_vars' => array(
'sz' => '160x600',
'fold' => 'atf',
'width' => '160',
'height' => '600',
'pos' => 'top',
)
),
// DFP Needs a header section tag
// We will use acm_output_html filter
// to do so
array(
'tag' => 'dfp_head',
'url' => '',
'url_vars' => array(
)
),
);
}
add_filter( 'acm_output_html','sp_acm_output_html', 5, 2 );
function sp_acm_output_html( $output_html, $tag_id ) {
switch( $tag_id ) {
case 'dfp_head':
$ad_tags = sp_ad_tags_ids( array() );
ob_start();
?>
<!-- Include google_services.js -->
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
<?php
foreach ( (array) $ad_tags as $tag ):
if ( $tag['tag'] == 'dfp_head' )
continue;
$tt = $tag['url_vars'];
?>
googletag.defineSlot('/%site_name%/<?php echo $tag['tag'] ?>', [<?php echo $tt['width'] ?>, <?php echo $tt['height'] ?>], "dfp-tag-<?php echo $tag['tag'] ?>").addService(googletag.pubads());
<?php
endforeach;
?>
//googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
<?php
$ltv_script = ob_get_clean();
break;
default:
$ltv_script = "
<div id='dfp-tag-%tag_id%' style='width:%width%px; height:%height%px;'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('dfp-tag-%tag_id%'); });
</script>
</div>
";
}
return $ltv_script;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment