Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setRequestNonPersonalizedAds #63

Closed
tienvooracht opened this issue May 29, 2018 · 13 comments
Closed

setRequestNonPersonalizedAds #63

tienvooracht opened this issue May 29, 2018 · 13 comments

Comments

@tienvooracht
Copy link

How would i get the plugin to use non personalized ads? Thanks!

@benlk
Copy link
Collaborator

benlk commented May 29, 2018

If you want to serve non-personalized ads to all users, the easiest way would be to use the DFP settings (https://support.google.com/dfp_premium/answer/7673898). By default, DFP will send personalized ads to non-European-Economic-Area users.

We don't currently have a way to do googletag.pubads().setRequestNonPersonalizedAds(1) for all readers using this plugin. It may be possible to enqueue JavaScript from your theme to access the googletag variable that DFP uses.

@tienvooracht
Copy link
Author

Any idea what would be the best way to do this? I've tried:

<script> googletag.cmd.push( function() { googletag.pubads().setRequestNonPersonalizedAds(1); googletag.enableServices(); }); </script>

Without any luck. Only managed to get it working once by enqueueing a custom version of jquery.dfp.js but that threw a lot of JS errors.

@benlk
Copy link
Collaborator

benlk commented May 30, 2018

I would write something like this:

<script>
window.googletag.pubads().setRequestNonPersonalizedAds(1);
</script>

And then wrap it in a PHP function, and add_action that function with priority 9 on wp_print_footer_scripts in order to get there before DoubleClick::footer_script

But that doesn't affect already-fetched ads, and might not trigger before the JS in DoubleClick::footer_script, so you may want to use disableInitialLoad, make the change, and then refresh().

I haven't tried this yet, but what you could attempt is setting disableInitialLoad via an argument on the jQuery( 'selector' ).dfp({}) call. Unfortunately there aren't any ways to do that without dequeueing DFW's code and enqueueing your own. You'd want to adjust the footer script:

public function footer_script() {
if ( ! $this->debug ) {
$mappings = array();
foreach ( $this->ad_slots as $ad ) {
if ( $ad->has_mapping() ) {
$mappings[ "mapping{$ad->id}" ] = $ad->mapping();
}
} ?>
<script type="text/javascript">
jQuery('.dfw-unit:not(.dfw-lazy-load)').dfp({
dfpID: '<?php echo esc_js( $this->network_code() ); ?>',
collapseEmptyDivs: false,
setTargeting: <?php echo wp_json_encode( $this->targeting() ); ?>,
sizeMapping: <?php echo wp_json_encode( $mappings ); ?>
});
</script>
<?php }
}

And in the lazy-loader in jquery.dfw.js:

if (toLoad.length > 0) {
$( toLoad ).dfp({
"dfpID": dfw.networkCode,
"collapseEmptyDivs": false,
"sizeMapping": dfw.mappings,
"setTargeting": dfw.targeting
}).addClass( 'dfw-loaded' );
}

An ideal revision to this plugin would be to set a filter on $data here:

$data = array(
'network_code' => $this->network_code,
'mappings' => $mappings,
'targeting' => $this->targeting(),
);
wp_localize_script( 'jquery.dfw.js', 'dfw', $data );
wp_enqueue_script( 'jquery.dfw.js' );

And then, once the filtered options are output via the script localization on the page as the global dfw variable, have the footer script draw from dfw instead of using its own hardcoded options here:

public function footer_script() {
if ( ! $this->debug ) {
$mappings = array();
foreach ( $this->ad_slots as $ad ) {
if ( $ad->has_mapping() ) {
$mappings[ "mapping{$ad->id}" ] = $ad->mapping();
}
} ?>
<script type="text/javascript">
jQuery('.dfw-unit:not(.dfw-lazy-load)').dfp({
dfpID: '<?php echo esc_js( $this->network_code() ); ?>',
collapseEmptyDivs: false,
setTargeting: <?php echo wp_json_encode( $this->targeting() ); ?>,
sizeMapping: <?php echo wp_json_encode( $mappings ); ?>
});
</script>
<?php }
}

Then as an example of how to use this, we'd need some docs about using window.googletag.pubads().refresh() to summon new ads once it's appropriate to load, and an example short plugin to put that in a popup to show how this might be used in a "can we load personalized ads y/n?" example, with the PHP filter and the js for the popup.

In conclusion, you're right, you'd need to customize this plugin some, but here's the list:

  • make a filter on $data before it's localised by calling apply_filters() on it
  • figure out how to get jquery.dfw.js to load arbitrary options from the dfw option, same as the footer script. What would probably work here is getting the keys on $data to be the appropriate keys for use as the configuration options that jQuery.dfp is looking for.
  • refactor DoubleClick::footer_script to draw from window.dfw instead of its own options

Please, if you have any questions about this response, let me know. This response not very well-edited and may be missing some code.

@tienvooracht
Copy link
Author

How about dequeueing the minified JS, enqueuing the non-minified JS and adding the setRequestNonPersonalizedAds(1) straight into the code (as a quick fix since i'm not making any progress).

@benlk
Copy link
Collaborator

benlk commented Jun 1, 2018

Yep, you could do that, and that does sound pretty easy.

@tienvooracht
Copy link
Author

No luck!

Any chance of making this an update for all of us EU based users of this plugin?

@benlk
Copy link
Collaborator

benlk commented Jun 15, 2018

We'll add it to the next milestone for this plugin, but I can't guarantee a release date for that fix.

Can you upload your edited version of the non-minified JS, please? Seeing what didn't work may help us get an update out faster.

@tienvooracht
Copy link
Author

I added:

pubadsService.setRequestNonPersonalizedAds(1);

Beneath:

if (dfpOptions.noFetch) { pubadsService.noFetch(); }

On line 297.

Ideal would be to have some kind of filter/hook that we can access, for example using our cookie consent script.

@benlk
Copy link
Collaborator

benlk commented Jun 18, 2018

Thanks for providing the edit.

What cookie consent script are you using?

@tienvooracht
Copy link
Author

It's a custom one based one, pretty straight forward. I've created a simple WordPress function that checks if consent is given. Ideal for showing/hiding ads. That's why a filter/hook would be awesome!

@tienvooracht
Copy link
Author

Any progress in adding this? Thanks!

@benlk
Copy link
Collaborator

benlk commented Oct 24, 2018

@tienvooracht Progress is starting in #81 and would be merged in the next 24 hours. What sorts of documentation would be helpful to you in using the filter added here? https://github.com/INN/doubleclick-for-wp/pull/81/files#diff-d3aa22c88317ac877d6ef91e82d40c08R172

@benlk
Copy link
Collaborator

benlk commented Nov 6, 2018

#81 is merged for 0.3.

@tienvooracht please let us know if the filter 'dfw_js_data' added in #81 works for you; I'm closing this issue for now but we can reopen it.

@benlk benlk closed this as completed Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants