Using dfp with Klaro consent manager

Created on 13 February 2025, about 2 months ago

Problem/Motivation

I need to use klaro together with dfp, I posted my strategy in #3506225 💬 Using with dfp module Active to get it working (and it works).
The thing is that maybe some parts could be added to dfp templates that will not be bad, like the use of Attribute class for script, the use of common class for each slot, and other ways to help with the implementation of klaro (or any other consent managers that must control scripts execution), at the end most of the sites that use dfp will need some consent managing...

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇪🇸Spain aleix

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @aleix
  • Merge request !27Issue #3506585 by aleix → (Closed) created by aleix
  • Pipeline finished with Failed
    about 2 months ago
    Total: 159s
    #423663
  • Pipeline finished with Failed
    about 2 months ago
    Total: 201s
    #423665
  • 🇪🇸Spain aleix

    !27 MR was using bad branch dest...
    !28 is the good one , with these changes, minor preprocess needs to be done to make it work with klaro. something like:

    /**
     * Implements hook_preprocess_HOOK() for dfp templates.
     */
    function dfp_klaro_preprocess_dfp_js_head_top(&$variables)
    {
      /** @var \Drupal\klaro\Utility\KlaroHelper $helper */
      $helper = \Drupal::service('klaro.helper');
    
      $service = $helper->matchKlaroApp($variables["google_tag_services_url"]);
      if ($service) {
        $variables['gads_dataset'] = [
          'type' => 'text/javascript',
          'name' => 'dfp_admanager',
          'src'  => $variables["google_tag_services_url"],
        ];
      }
    }
    
    /**
     * Implements hook_preprocess_HOOK() for dfp templates.
     */
    function dfp_klaro_preprocess_dfp_short_tag(&$variables)
    {
      /** @var \Drupal\klaro\Utility\KlaroHelper $helper */
      $helper = \Drupal::service('klaro.helper');
      $service = $helper->matchKlaroApp(TagInterface::GOOGLE_TAG_SERVICES_URL);
      if ($service) {
        $variables['image_attributes'] = $helper->rewriteAttributes($variables['image_attributes'], $service->id());
      }
    }
    
    /**
     * Implements hook_preprocess_HOOK() for dfp templates.
     */
    function dfp_klaro_preprocess_dfp_tag(&$variables)
    {
      _dfp_klaro_preprocess_dfp($variables);
    }
    
    /**
     * Implements hook_preprocess_HOOK() for dfp templates.
     */
    function dfp_klaro_preprocess_dfp_js_head_bottom(&$variables)
    {
      _dfp_klaro_preprocess_dfp($variables);
    }
    
    /**
     * Implements hook_preprocess_HOOK() for dfp templates.
     */
    function dfp_klaro_preprocess_dfp_slot_definition_js(&$variables)
    {
      _dfp_klaro_preprocess_dfp($variables);
    }
    
    /**
     * Adds the klaro attributes to each script preprocessed by dfp.
     */
    function _dfp_klaro_preprocess_dfp(&$variables)
    {
      /** @var \Drupal\klaro\Utility\KlaroHelper $helper */
      $helper = \Drupal::service('klaro.helper');
      $service = $helper->matchKlaroApp(TagInterface::GOOGLE_TAG_SERVICES_URL);
      if ($service) {
        $variables['script_attributes'] = $helper->rewriteAttributes($variables['script_attributes'], $service->id());
      }
    }
    
    
  • Pipeline finished with Success
    about 2 months ago
    Total: 372s
    #423674
Production build 0.71.5 2024