You are here

simple_facebook_pixel.module in Simple Facebook Pixel 8

Simple Facebook Pixel.

Adds the required Javascript to your site to allow tracking by the Facebook Pixel.

File

simple_facebook_pixel.module
View source
<?php

/**
 * @file
 * Simple Facebook Pixel.
 *
 * Adds the required Javascript to your site to allow tracking by the Facebook
 * Pixel.
 */
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function simple_facebook_pixel_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.simple_facebook_pixel':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Simple Facebook Pixel installs Facebook Pixel on your Drupal site.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_page_attachments().
 */
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',
  ];
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function simple_facebook_pixel_user_insert(EntityInterface $entity) {

  /** @var \Drupal\simple_facebook_pixel\PixelBuilderService $pixel_builder */
  $pixel_builder = \Drupal::service('simple_facebook_pixel.pixel_builder');
  if (!$pixel_builder
    ->isEnabled()) {
    return;
  }
  $config = \Drupal::config('simple_facebook_pixel.settings');
  $complete_registration_enabled = $config
    ->get('complete_registration_enabled');
  if (!$complete_registration_enabled) {
    return;
  }
  $data = [
    'content_name' => 'registration',
    'status' => 'complete',
  ];
  $pixel_builder
    ->addEvent('CompleteRegistration', $data, TRUE);
}