public

DFP Small Business

  • Download Gist
acm-dfp-small-business-config.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.