You are here

function simple_facebook_pixel_page_attachments in Simple Facebook Pixel 8

Implements hook_page_attachments().

File

./simple_facebook_pixel.module, line 32
Simple Facebook Pixel.

Code

function simple_facebook_pixel_page_attachments(array &$page) {
  $config = \Drupal::config('simple_facebook_pixel.settings');
  $page_cache_tags = isset($page['#cache']['tags']) ? $page['#cache']['tags'] : [];
  $cache_tags = Cache::mergeTags($page_cache_tags, $config
    ->getCacheTags());
  $page['#cache']['tags'] = Cache::mergeTags($cache_tags, [
    'simple_facebook_pixel:complete_registration',
    'simple_facebook_pixel:add_to_cart',
    'simple_facebook_pixel:add_to_wishlist',
    'simple_facebook_pixel:purchase',
  ]);

  /** @var \Drupal\simple_facebook_pixel\PixelBuilderService $pixel_builder */
  $pixel_builder = \Drupal::service('simple_facebook_pixel.pixel_builder');
  if (!$pixel_builder
    ->isEnabled()) {
    return;
  }

  /** @var \Drupal\simple_facebook_pixel\PageContextServiceInterface $page_context */
  $page_context = \Drupal::service('simple_facebook_pixel.page_context');
  $page_context
    ->build();
  $pixel_script_code = $pixel_builder
    ->getPixelScriptCode();
  $pixel_noscript_code = $pixel_builder
    ->getPixelNoScriptCode();
  $page['#attached']['html_head'][] = [
    [
      '#tag' => 'script',
      '#value' => $pixel_script_code,
    ],
    'simple_facebook_pixel_script_code',
  ];
  $page['#attached']['html_head'][] = [
    [
      '#type' => 'inline_template',
      '#template' => $pixel_noscript_code,
    ],
    'simple_facebook_pixel_noscript_code',
  ];
}