INN / doubleclick-for-wp

WordPress plugin for serving Google Ad Manager ads
https://wordpress.org/plugins/doubleclick-for-wp/
GNU General Public License v2.0
25 stars 11 forks source link

setRequestNonPersonalizedAds #63

Closed tienvooracht closed 5 years ago

tienvooracht commented 6 years ago

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

benlk commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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:

https://github.com/INN/doubleclick-for-wp/blob/d90fba39cb0b14cbcf94d3cc86d98ec84108a9ba/dfw-init.php#L187-L204

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

https://github.com/INN/doubleclick-for-wp/blob/ac6a82170437881ade6a0e56751a6876866cbd87/js/jquery.dfw.js#L32-L39

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

https://github.com/INN/doubleclick-for-wp/blob/d90fba39cb0b14cbcf94d3cc86d98ec84108a9ba/dfw-init.php#L159-L166

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: https://github.com/INN/doubleclick-for-wp/blob/d90fba39cb0b14cbcf94d3cc86d98ec84108a9ba/dfw-init.php#L187-L204

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:

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 commented 6 years ago

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 commented 6 years ago

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

tienvooracht commented 6 years ago

No luck!

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

benlk commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Thanks for providing the edit.

What cookie consent script are you using?

tienvooracht commented 6 years ago

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 commented 5 years ago

Any progress in adding this? Thanks!

benlk commented 5 years ago

@tienvooracht Progress is starting in https://github.com/INN/doubleclick-for-wp/pull/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 commented 5 years ago

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.