Skip to content

Commit

Permalink
feat(ads): support network targeting key-val (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe authored Nov 27, 2023
1 parent 07b5470 commit 37f677a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
68 changes: 68 additions & 0 deletions includes/class-gam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Newspack Ads GAM Integration.
*
* Implements support for custom targeting key-val pairs for each site in the
* network. The ad slots are then targeted to the site's URL.
*
* @package Newspack
*/

namespace Newspack_Network;

use Newspack_Ads\Providers\GAM\Api as GAM_API;
use Newspack_Ads\Providers\GAM_Model;

/**
* Integration class for Newspack Ads' GAM support.
*/
final class GAM {
/**
* Initialize hooks.
*/
public static function init() {
add_action( 'newspack_ads_setup_gam', [ __CLASS__, 'create_targeting_keys' ] );
add_action( 'save_post_' . Hub\Nodes::POST_TYPE_SLUG, [ __CLASS__, 'create_targeting_keys' ] );
add_filter( 'newspack_ads_ad_targeting', [ __CLASS__, 'add_targeting' ], 10, 2 );
}

/**
* Create targeting keys.
*/
public static function create_targeting_keys() {
if ( ! Site_Role::is_hub() ) {
return;
}
if ( ! class_exists( 'Newspack_Ads\Providers\GAM_Model' ) ) {
return;
}
$api = GAM_Model::get_api();
if ( ! $api || is_wp_error( $api ) ) {
Debugger::log( 'Error adding GAM targeting keys: GAM API is not available.' );
return;
}
$nodes = Hub\Nodes::get_all_nodes();
$node_urls = array_map(
function( $node ) {
return $node->get_url();
},
$nodes
);
$urls = array_merge( [ \get_site_url() ], $node_urls );
$api->targeting_keys->create_targeting_key( 'site', $urls, 'PREDEFINED', 'CUSTOM_DIMENSION' );
Debugger::log( 'Updated GAM targeting keys.' );
}

/**
* Add targeting.
*
* @param array $targeting Targeting.
* @param array $ad_unit Ad unit.
*
* @return array
*/
public static function add_targeting( $targeting, $ad_unit ) {
$targeting['site'] = \get_site_url();
return $targeting;
}
}
3 changes: 2 additions & 1 deletion includes/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public static function init() {
Rest_Authenticaton::init_node_filters();
}
}

Data_Listeners::init();
Reader_Roles_Filter::init();
GAM::init();

register_activation_hook( NEWSPACK_NETWORK_PLUGIN_FILE, [ __CLASS__, 'activation_hook' ] );
}
Expand Down

0 comments on commit 37f677a

Please sign in to comment.