You are here

function cookies_ga_page_attachments in COOKiES Consent Management 1.0.x

Implements hook_page_attachments().

File

modules/cookies_ga/cookies_ga.module, line 31
Contains cookies_ga.module.

Code

function cookies_ga_page_attachments(&$page) {
  $doKo = CookiesKnockOutService::getInstance()
    ->doKnockOut();
  if ($doKo && isset($page["#attached"]["html_head"])) {
    foreach ($page["#attached"]["html_head"] as $key => $head_tag) {
      $scripts = [
        'google_analytics_tracking_script',
        'google_analytics_tracking_file',
      ];
      foreach ($scripts as $script) {
        if (in_array($script, $head_tag)) {
          $attr = isset($head_tag[0]['#attributes']) ? $head_tag[0]['#attributes'] : [];
          $attr = array_merge($attr, [
            'type' => 'application/json',
            'id' => "cookies_{$script}",
          ]);
          $page["#attached"]["html_head"][$key][0]['#attributes'] = new Attribute($attr);
          $page["#attached"]["library"][] = 'cookies_ga/analytics';
          break;
        }
      }
    }
  }
}