function pardot_page_attachments in Pardot Integration 8
Same name and namespace in other branches
- 2.x pardot.module \pardot_page_attachments()
Implements hook_page_attachments().
Adds Pardot tracking script with configuration settings when appropriate.
File
- ./
pardot.module, line 16 - Pardot integration module.
Code
function pardot_page_attachments(array &$page) {
// Get conditional state variable.
// @see \Drupal\pardot\EventSubscriber\PardotEventSubscriber
$include_tracking = (bool) \Drupal::state()
->get('pardot.include_tracking') ?: 0;
$config = \Drupal::config('pardot.settings');
if (null !== $config
->get('account_id') && $include_tracking) {
// Use default campaign ID.
$campaign_id = $config
->get('default_campaign_id');
// Match current path to any existing campaigns and use its campaign ID.
$storage = \Drupal::entityManager()
->getStorage('pardot_campaign');
$campaigns = $storage
->loadMultiple();
if (!empty($campaigns)) {
$current_path = \Drupal::service('path.current')
->getPath();
foreach ($campaigns as $campaign) {
if (\Drupal::service('path.matcher')
->matchPath($current_path, $campaign->pages['pages'])) {
$campaign_id = $campaign->campaign_id;
}
}
}
$page['#attached']['drupalSettings']['pardot']['accountId'] = $config
->get('account_id');
$page['#attached']['drupalSettings']['pardot']['campaignId'] = $campaign_id;
$page['#attached']['library'][] = 'pardot/pardot';
// Match current path to any existing scores and use its value.
$storage = \Drupal::entityManager()
->getStorage('pardot_score');
$scores = $storage
->loadMultiple();
if (!empty($scores)) {
$current_path = \Drupal::service('path.current')
->getPath();
foreach ($scores as $score) {
if (\Drupal::service('path.matcher')
->matchPath($current_path, $score->pages['pages'])) {
$score_value = $score->score_value;
}
}
}
if (isset($score_value)) {
$page['#attached']['drupalSettings']['pardot']['score'] = $score_value;
}
}
}