You are here

public function PixelBuilderService::getPixelScriptCode in Simple Facebook Pixel 8

Gets pixel script code.

Return value

string The Facebook Pixel script code.

Overrides PixelBuilderServiceInterface::getPixelScriptCode

File

src/PixelBuilderService.php, line 148

Class

PixelBuilderService
Class PixelBuilderService.

Namespace

Drupal\simple_facebook_pixel

Code

public function getPixelScriptCode() {
  $pixels = $this
    ->getPixelIds();
  $pixels = array_map(function ($pixel_id) {
    return "fbq('init', '" . trim($pixel_id) . "')";
  }, $pixels);
  $pixels = implode(' ', $pixels);
  $pixel_script_code = str_replace('{{pixels}}', $pixels, self::FACEBOOK_PIXEL_CODE_SCRIPT);
  $events = $this
    ->getEvents();

  // Allow other modules to alter the events array.
  $this->moduleHandler
    ->alter('simple_facebook_pixel_events', $events);
  foreach ($events as $event) {
    $pixel_script_code .= 'fbq("track", "' . $event['event'] . '", ' . json_encode($event['data']) . ');';
  }

  // Allow other modules to alter the script code.
  $this->moduleHandler
    ->alter('simple_facebook_pixel_script_code', $pixel_script_code);
  return $pixel_script_code;
}