Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added targeting to DFP async provider #81

Closed
wants to merge 10 commits into from

3 participants

@harvitronix

Modified the DFP async provider code to pass keywords, title, path and domain to the defineSlot method. This will allow users to target specific ads based on these attributes through DFP itself.

@rinatkhaziev
Collaborator

I don't think this will work.
What if I want to target by custom taxonomy?
What if I don't want to set targeting at all?
What if I want to do targeting by some weird parameters?

Here are some real life examples:

.setTargeting( "pos", "atf" );
.setTargeting( "right", "top" );
.setTargeting( "pixel", "1" );

Perhaps it's better to add a filter for targeting and make the value empty by default.

@harvitronix

Yes, I'd have to agree. Should instead be a filter.

@harvitronix

I've instead made target setting filterable (with a default of empty). Not sure that this is the correct place to put the apply_filters line, though.

@rinatkhaziev
Collaborator

@harvitronix How about something like this:
https://gist.github.com/rinatkhaziev/6063442

The idea is to make targeting parameters an array, and then apply filters to it, passing array for filtering and tag id for any additional logic. After that, format a string with a special method, making sure that passed parameters are validated and escaped.

@harvitronix

@rinatkhaziev Realized we may have an issue with esc_js on the value output. The value sent to the targeting key will often need to be a JS array. Escaping it will turn ['foo','bar'] into [\'foo\',\'bar\'] which will trip up the function, I believe.

@jeffstieler jeffstieler commented on the diff
providers/doubleclick-for-publishers-async.php
@@ -204,6 +212,24 @@ public function action_wp_head() {
do_action( 'acm_tag', 'dfp_head' );
}
+ /**
+ * Format setTargeting string
+ * @param array $params_array Key value pair you'd like to target. Example: ["title"]=>"Your post's title"
+ * @return string The targeting string to append to slot definition
+ */
+ function format_targeting_string( $params_array = array() ) {
+ $ret = '';
+ var_dump($params_array);
@jeffstieler Owner

@harvitronix You probably want to kill this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jeffstieler jeffstieler referenced this pull request from a commit in voceconnect/Ad-Code-Manager
@jeffstieler jeffstieler Automattic#81 ac6e39d
@harvitronix

Closing this PR as it's stale and not structured well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 7 deletions.
  1. +33 −7 providers/doubleclick-for-publishers-async.php
View
40 providers/doubleclick-for-publishers-async.php
@@ -139,6 +139,14 @@ public function filter_output_html( $output_html, $tag_id ) {
switch ( $tag_id ) {
case 'dfp_head':
$ad_tags = $ad_code_manager->ad_tag_ids;
+
+ // Allow users to set targeting parameters
+ $targeting_string = '';
+ $targeting_params_array = apply_filters( 'acm_targeting_params', array() );
+ if ( ! empty( $targeting_params_array ) ) {
+ $targeting_string = $this->format_targeting_string( $targeting_params_array );
+ }
+
ob_start();
?>
<!-- Include google_services.js -->
@@ -164,15 +172,15 @@ public function filter_output_html( $output_html, $tag_id ) {
continue;
$tt = $tag['url_vars'];
- $matching_ad_code = $ad_code_manager->get_matching_ad_code( $tag['tag'] );
- if ( ! empty( $matching_ad_code ) ) {
- // @todo There might be a case when there are two tags registered with the same dimensions
- // and the same tag id ( which is just a div id ). This confuses DFP Async, so we need to make sure
- // that tags are unique
+ $matching_ad_code = $ad_code_manager->get_matching_ad_code( $tag['tag'] );
+ if ( ! empty( $matching_ad_code ) ) {
+ // @todo There might be a case when there are two tags registered with the same dimensions
+ // and the same tag id ( which is just a div id ). This confuses DFP Async, so we need to make sure
+ // that tags are unique
?>
-googletag.defineSlot('/<?php echo esc_attr( $matching_ad_code['url_vars']['dfp_id'] ); ?>/<?php echo esc_attr( $matching_ad_code['url_vars']['tag_name'] ); ?>', [<?php echo (int)$tt['width'] ?>, <?php echo (int)$tt['height'] ?>], "acm-ad-tag-<?php echo esc_attr( $matching_ad_code['url_vars']['tag_id'] ); ?>").addService(googletag.pubads());
+googletag.defineSlot('/<?php echo esc_attr( $matching_ad_code['url_vars']['dfp_id'] ); ?>/<?php echo esc_attr( $matching_ad_code['url_vars']['tag_name'] ); ?>', [<?php echo (int)$tt['width'] ?>, <?php echo (int)$tt['height'] ?>], "acm-ad-tag-<?php echo esc_attr( $matching_ad_code['url_vars']['tag_id'] ); ?>").addService(googletag.pubads())<?php echo $targeting_string; ?>;
<?php
- }
+ }
endforeach;
?>
googletag.pubads().enableSingleRequest();
@@ -204,6 +212,24 @@ public function action_wp_head() {
do_action( 'acm_tag', 'dfp_head' );
}
+ /**
+ * Format setTargeting string
+ * @param array $params_array Key value pair you'd like to target. Example: ["title"]=>"Your post's title"
+ * @return string The targeting string to append to slot definition
+ */
+ function format_targeting_string( $params_array = array() ) {
+ $ret = '';
+ var_dump($params_array);
@jeffstieler Owner

@harvitronix You probably want to kill this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ // Iterate over array of key value pairs and format a string
+ foreach( (array) $params_array as $key => $value ) {
+ if ( ! empty( $key ) && ! empty( $value ) ) {
+ $ret .= ".setTargeting('" . esc_js( $key ) ."','" . esc_js( $value ) . "')";
+ }
+ }
+
+ return $ret;
+ }
+
}
class Doubleclick_For_Publishers_Async_ACM_WP_List_Table extends ACM_WP_List_Table {
Something went wrong with that request. Please try again.