function facebook_pixel_preprocess_page in Facebook Pixel 7
Implements hook_preprocess_page().
See also
File
- ./
facebook_pixel.module, line 127 - Drupal Module: Facebook Pixel.
Code
function facebook_pixel_preprocess_page(&$variables) {
global $user;
if (variable_get('facebook_pixel_exclude_admin_paths', 1) && path_is_admin(current_path()) || !facebook_pixel_user_visibility($user)) {
return;
}
if ($pixel_id = variable_get('facebook_pixel_id')) {
$script = "\n<!-- Facebook Pixel Code -->\n<script>\n!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?\nn.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;\nn.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;\nt.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,\ndocument,'script','//connect.facebook.net/en_US/fbevents.js');\n\nfbq('init', '{$pixel_id}');\nfbq('track', 'PageView');\n";
if (isset($_SESSION['facebook_pixel']) && is_array($_SESSION['facebook_pixel'])) {
foreach ($_SESSION['facebook_pixel'] as $event) {
if (strpos($event, ':') !== FALSE) {
list($event, $entity_id) = explode(':', $event);
}
if (!empty($entity_id) && function_exists('facebook_pixel_data_' . $event)) {
$data = call_user_func('facebook_pixel_data_' . $event, $entity_id);
// Allow to alter data.
drupal_alter('facebook_pixel_data_' . $event, $data, $entity_id);
$script .= "fbq('track', '{$event}', " . drupal_json_encode($data) . ");\n";
}
else {
$script .= "fbq('track', '{$event}');\n";
}
}
unset($_SESSION['facebook_pixel']);
}
$script .= "</script>\n<noscript><img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id={$pixel_id}&ev=PageView&noscript=1' /></noscript>\n<!-- End Facebook Pixel Code -->\n";
$element = array(
'#type' => 'markup',
'#markup' => $script,
'#weight' => 1000,
);
drupal_add_html_head($element, 'facebook_pixel');
}
}