Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. function my_landing_page_preprocess(&$variables) {
  2. if (isset($variables['entity'])) {
  3. $node = $variables['entity'];
  4.  
  5. my_analytics_get_landing_page_analytics($node);
  6. }
  7. }
  8.  
  9. function my_analytics_get_landing_page_analytics($node) {
  10. if ($node instanceof Node && $node->getEntityTypeId() == 'node' && $node->getType() == 'landing_page') {
  11.  
  12. if ($node->hasField('field_page_type') && count($node->get('field_page_type')->getValue()) > 0) {
  13. my_analytics_add_tags_global([
  14. 'TagID_channel' => $node->get('field_page_type')->getValue()[0]['value'],
  15. ]);
  16. }
  17.  
  18. }
  19. }
  20.  
  21. function my_analytics_add_tags_global(array $tags) {
  22. if (isset($GLOBALS['my_dtm_global_tags']) && is_array($GLOBALS['my_dtm_global_tags'])) {
  23. $GLOBALS['my_dtm_global_tags'] = array_merge($GLOBALS['my_dtm_global_tags'], $tags);
  24. }
  25. else {
  26. $GLOBALS['my_dtm_global_tags'] = $tags;
  27. }
  28. }
  29.  
  30. function my_analytics_add_set_tags(array &$build, array $tags) {
  31. if (!empty($build['#attached']['drupalSettings']['my_analytics']['setTags'])) {
  32. $build['#attached']['drupalSettings']['my_analytics'['setTags'] = [];
  33. }
  34.  
  35. // Search for global tags added from other modules.
  36. if (isset($GLOBALS['my_dtm_global_tags']) && is_array($GLOBALS['my_dtm_global_tags'])) {
  37. $tags = array_merge($tags, $GLOBALS['my_dtm_global_tags']);
  38. }
  39.  
  40. foreach ($tags as $tag_id => $tag_value) {
  41. $build['#attached']['drupalSettings']['my_analytics']['setTags'][] = [
  42. 'id' => $tag_id,
  43. 'value' => $tag_value,
  44. ];
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement