public function GoogleAdwordsTracker::attachTrackingToElement in Google AdWords Conversion Tracking 8
Add tracking JS to a render element
@returns null
Parameters
array $element: Render element onto which #js will be attached
Boolean $empty: Empty tracking list after attaching?
Throws
\Exception If a non-array is passed as an argument
File
- src/
GoogleAdwordsTracker.php, line 90 - Contains Drupal\google_adwords\GoogleAdwordsTracker.
Class
- GoogleAdwordsTracker
- Class GoogleAdwordsTracker.
Namespace
Drupal\google_adwordsCode
public function attachTrackingToElement(&$element, $empty = TRUE) {
if (!static::hasTracking()) {
return;
}
if (!is_array($element)) {
/**
* @todo create custom exception for this
*/
throw new \Exception('Non render array passed for GoogleAdwords Tracking JS attachments');
}
/**
* @var \Drupal\Core\Config\ImmutableConfig $config
* saved settings for google_adwords
*/
$config = \Drupal::config('google_adwords.settings');
// make sure that our adwords js is loaded ( @see google_adwords.libraries.yml )
// #page won't accept JS anymore : element['#attached']['js']['external-google-adwords-script'] = $config->get('external_script'); // 'https://www.googleadservices.com/pagead/conversion.js';
$element['#attached']['library'][] = 'google_adwords/google_adwords.tracker';
$element['#attached']['library'][] = 'node/drupal.node.admin';
// Add our JS values array
$element['#attached']['drupalSettings']['google_adwords'] = [
'defaults' => [
'conversion_id' => $config
->get('conversion_id'),
'label' => $config
->get('label'),
'language' => $config
->get('language'),
'color' => $config
->get('color'),
'format' => $config
->get('format'),
],
'trackings' => static::$trackings,
];
// empty our trackings in case this
if ($empty) {
static::$trackings = [];
}
}