Giter Site home page Giter Site logo

Comments (13)

benlk avatar benlk commented on June 25, 2024

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.

from doubleclick-for-wp.

tienvooracht avatar tienvooracht commented on June 25, 2024

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.

from doubleclick-for-wp.

benlk avatar benlk commented on June 25, 2024

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.

from doubleclick-for-wp.

tienvooracht avatar tienvooracht commented on June 25, 2024

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).

from doubleclick-for-wp.

benlk avatar benlk commented on June 25, 2024

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

from doubleclick-for-wp.

tienvooracht avatar tienvooracht commented on June 25, 2024

No luck!

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

from doubleclick-for-wp.

benlk avatar benlk commented on June 25, 2024

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.

from doubleclick-for-wp.

tienvooracht avatar tienvooracht commented on June 25, 2024

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.

from doubleclick-for-wp.

benlk avatar benlk commented on June 25, 2024

Thanks for providing the edit.

What cookie consent script are you using?

from doubleclick-for-wp.

tienvooracht avatar tienvooracht commented on June 25, 2024

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!

from doubleclick-for-wp.

tienvooracht avatar tienvooracht commented on June 25, 2024

Any progress in adding this? Thanks!

from doubleclick-for-wp.

benlk avatar benlk commented on June 25, 2024

@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

from doubleclick-for-wp.

benlk avatar benlk commented on June 25, 2024

#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.

from doubleclick-for-wp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.