Skip to content

Instantly share code, notes, and snippets.

@rinatkhaziev
Created January 18, 2012 05:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rinatkhaziev/1631131 to your computer and use it in GitHub Desktop.
Save rinatkhaziev/1631131 to your computer and use it in GitHub Desktop.
ACM DFP Setup
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment