Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function my_landing_page_preprocess(&$variables) {
- if (isset($variables['entity'])) {
- $node = $variables['entity'];
- my_analytics_get_landing_page_analytics($node);
- }
- }
- function my_analytics_get_landing_page_analytics($node) {
- if ($node instanceof Node && $node->getEntityTypeId() == 'node' && $node->getType() == 'landing_page') {
- if ($node->hasField('field_page_type') && count($node->get('field_page_type')->getValue()) > 0) {
- my_analytics_add_tags_global([
- 'TagID_channel' => $node->get('field_page_type')->getValue()[0]['value'],
- ]);
- }
- }
- }
- function my_analytics_add_tags_global(array $tags) {
- if (isset($GLOBALS['my_dtm_global_tags']) && is_array($GLOBALS['my_dtm_global_tags'])) {
- $GLOBALS['my_dtm_global_tags'] = array_merge($GLOBALS['my_dtm_global_tags'], $tags);
- }
- else {
- $GLOBALS['my_dtm_global_tags'] = $tags;
- }
- }
- function my_analytics_add_set_tags(array &$build, array $tags) {
- if (!empty($build['#attached']['drupalSettings']['my_analytics']['setTags'])) {
- $build['#attached']['drupalSettings']['my_analytics'['setTags'] = [];
- }
- // Search for global tags added from other modules.
- if (isset($GLOBALS['my_dtm_global_tags']) && is_array($GLOBALS['my_dtm_global_tags'])) {
- $tags = array_merge($tags, $GLOBALS['my_dtm_global_tags']);
- }
- foreach ($tags as $tag_id => $tag_value) {
- $build['#attached']['drupalSettings']['my_analytics']['setTags'][] = [
- 'id' => $tag_id,
- 'value' => $tag_value,
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement