Skip to content

Commit

Permalink
feat: add support for ad targeting via filter
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiulodro committed Apr 30, 2020
1 parent f458ee9 commit 9abc0ae
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions includes/class-newspack-ads-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,21 @@ function( $item ) {
?>
<script>
googletag.cmd.push(function() {
var ad_units = [];
<?php foreach ( Newspack_Ads_Model::$ad_ids as $unique_id => $ad_unit ) : ?>
<?php if ( $ad_unit['responsive'] ) : ?>
<?php foreach ( $ad_unit['sizes'] as $size ) : ?>
googletag.defineSlot('/<?php echo esc_attr( $network_code ); ?>/<?php echo esc_attr( $ad_unit['code'] ); ?>', [ [ <?php echo absint( $size[0] ); ?>, <?php echo absint( $size[1] ); ?> ] ], 'div-gpt-<?php echo esc_attr( $ad_unit['code'] ); ?>-<?php echo esc_attr( $unique_id ); ?>-<?php echo absint( $size[0] ); ?>x<?php echo absint( $size[1] ); ?>').addService(googletag.pubads());
ad_units['<?php echo esc_attr( $ad_unit['code'] ); ?>'] = googletag.defineSlot('/<?php echo esc_attr( $network_code ); ?>/<?php echo esc_attr( $ad_unit['code'] ); ?>', [ [ <?php echo absint( $size[0] ); ?>, <?php echo absint( $size[1] ); ?> ] ], 'div-gpt-<?php echo esc_attr( $ad_unit['code'] ); ?>-<?php echo esc_attr( $unique_id ); ?>-<?php echo absint( $size[0] ); ?>x<?php echo absint( $size[1] ); ?>').addService(googletag.pubads());
<?php endforeach; ?>
<?php else : ?>
googletag.defineSlot('/<?php echo esc_attr( $network_code ); ?>/<?php echo esc_attr( $ad_unit['code'] ); ?>', [ <?php echo esc_attr( implode( ',', $formatted_sizes[ $unique_id ] ) ); ?> ], 'div-gpt-ad-<?php echo esc_attr( $unique_id ); ?>-0').addService(googletag.pubads());
ad_units['<?php echo esc_attr( $ad_unit['code'] ); ?>'] = googletag.defineSlot('/<?php echo esc_attr( $network_code ); ?>/<?php echo esc_attr( $ad_unit['code'] ); ?>', [ <?php echo esc_attr( implode( ',', $formatted_sizes[ $unique_id ] ) ); ?> ], 'div-gpt-ad-<?php echo esc_attr( $unique_id ); ?>-0').addService(googletag.pubads());
<?php endif; ?>

<?php $targeting = apply_filters( 'newspack_ads_ad_targeting', [], $ad_unit ); ?>
<?php if ( ! empty( $targeting ) ) : ?>
<?php foreach ( $targeting as $key => $val ) : ?>
ad_units['<?php echo esc_attr( $ad_unit['code'] ); ?>'].setTargeting( '<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( $val ); ?>' );
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
googletag.pubads().enableSingleRequest();
Expand Down

0 comments on commit 9abc0ae

Please sign in to comment.