public
Last active

ACM DFP Setup

  • Download Gist
acm_dfp.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
<?php
// Whitelist default double click url
add_filter( 'acm_whitelisted_script_urls', 'rk_acm_whiltelisted_script_urls');
function rk_acm_whiltelisted_script_urls( $whitelisted_urls ) {
$whitelisted_urls = array( 'ad.doubleclick.net' );
return $whitelisted_urls;
}
 
// Tokenize url
add_filter( 'acm_default_url', 'rk_acm_default_url' ) ;
function rk_acm_default_url( $url ) {
if ( 0 === strlen( $url ) ) {
return "http://ad.doubleclick.net/adj/%site_name%/%zone1%;s1=%zone1%;s2=;pid=%permalink%;fold=%fold%;kw=;test=%test%;ltv=ad;pos=%pos%;dcopt=%dcopt%;tile=%tile%;sz=%sz%;";
}
}
// Add additional output tokens
add_filter('acm_output_tokens', 'rk_acm_output_tokens', 5, 3 );
/**
* Let's fine tune our output tokens
* This is the real example of
* how easily you can modify output
* depending on your ad network specs
*/
function rk_acm_output_tokens( $output_tokens, $tag_id, $code_to_display ) {
global $dfp_tile;
global $dfp_ord;
global $dfp_pos;
global $dfp_dcopt;
global $wp_query;
// We can't really rely on get_permalink() so use $_SERVER['REQUEST_URI] as bulletproof solution for generating unique pids
$link = strlen( $_SERVER['REQUEST_URI'] ) > 1 ? sanitize_key( $_SERVER['REQUEST_URI'] ) : home_url();
$output_tokens['%permalink%'] = str_replace( array( '/',':', '.' ), "", $link );
$output_tokens['%random%'] = $dfp_ord;
$output_tokens['%tile%'] = ++$dfp_tile;
if ( false === $dfp_pos[ $code_to_display['url_vars']['sz'] ] ) {
$output_tokens['%pos%'] = 'top';
$dfp_pos[ $code_to_display['url_vars']['sz'] ] = true;
} else {
$output_tokens['%pos%'] = 'bottom';
}
if ( ! $dfp_dcopt ) {
$output_tokens['%dcopt%'] = 'ist';
$dfp_dcopt = true;
} else {
$output_tokens['%dcopt%'] = '';
}
$output_tokens['%test%'] = isset( $_GET['test'] ) && $_GET['test'] == 'on' ? 'on' : '';
return $output_tokens;
}
 
add_filter( 'acm_ad_tag_ids', 'rk_ad_tags_ids' );
/**
* Let's expand default list of ad_tag_ids
* with 1x1 tag used for richmedia ads
*/
function rk_ad_tags_ids( $ad_tag_ids ) {
return array(
array(
'tag' => '728x90-atf',
'url_vars' => array(
'sz' => '728x90',
'fold' => 'atf',
'width' => '728',
'height' => '90',
)
),
array(
'tag' => '728x90-btf',
'url_vars' => array(
'sz' => '728x90',
'fold' => 'btf',
'width' => '728',
'height' => '90',
)
) ,
array(
'tag' => '300x250-atf',
'url_vars' => array(
'sz' => '300x250',
'fold' => 'atf',
'width' => '300',
'height' => '250',
)
),
array(
'tag' => '300x250-btf',
'url_vars' => array(
'sz' => '300x250',
'fold' => 'btf',
'width' => '300',
'height' => '250',
)
),
array(
'tag' => '160x600-atf',
'url_vars' => array(
'sz' => '160x600',
'fold' => 'atf',
'width' => '160',
'height' => '600',
)
),
array(
'tag' => '1x1',
'url_vars' => array(
'sz' => '1x1',
'fold' => 'int',
'pos' => 'top',
)
)
);
}
 
add_filter( 'acm_output_html','rk_acm_output_html', 5, 2 );
function rk_acm_output_html( $output_html, $tag_id ) {
$ltv_script = '<!-- DFP %pos% %sz% ad tag -->
<script language="JavaScript" type="text/javascript">
if (typeof ord==\'undefined\') {ord=Math.random()*10000000000000000;}
if (typeof(dfp_tile) == \'undefined\') dfp_tile=%tile%;
document.write(\'<script language="JavaScript" src="%url%ord=\' + ord + \'?" type="text/javascript"><\/script>\');
</script><noscript><a href="%url%ord=%random%?" target="_blank"><img src="%url%ord=%random%?" width="%width%" height="%height%" border="0" alt=""></a></noscript>
<!-- //DFP %pos% %sz% tag -->';
return $ltv_script;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.