-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasu_brand.gtm.inc
71 lines (61 loc) · 1.87 KB
/
asu_brand.gtm.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* @file
* Contains the GTM snippet insertion code.
*
* @author Chandi Cumaranatunge
*/
/*********************************************************************
DRUPAL HOOKS
*********************************************************************/
/**
* Implements callback_post_render().
*
* Inserts GTM snippet as immediate child of the opening body tag.
*/
function asu_brand_page_process(&$children, $elements) {
if (asu_brand_is_header_visible($elements)) {
// Added check for testing - Hide GTM code during testing
if (variable_get('asu_brand_ci_testing', 0) != 1 ) {
$custom_gtm = variable_get('asu_brand_gtm_override', '');
$gtm = (trim($custom_gtm) != '') ? $custom_gtm : asu_brand_get_gtm_snippet();
}
else {
$gtm = '<!--GTM Code removed for testing -->';
}
// Insert snippet after the opening body tag.
if ($gtm) {
$children = preg_replace('@<body[^>]*>@', '$0' . $gtm, $children, 1);
}
}
return $children;
}
/*********************************************************************
INTERNAL
*********************************************************************/
/**
* Checks if ASU Brand header is visible on current page
* @param array $elements
* Render array of page elements
*/
function asu_brand_is_header_visible($elements) {
$module_name = basename(__FILE__, '.gtm.inc');
foreach ($elements as $region_key => $region) {
if (($region_key[0] != '#') && is_array($region)) {
foreach ($region as $block_key => $block) {
if ($block_key == $module_name.'_'.ASU_BRAND_HEADER_DELTA) {
return TRUE;
}
}
}
}
return false;
}
/**
* Get GTM snippet from header assets
*/
function asu_brand_get_gtm_snippet() {
$settings = asu_brand_get_block_settings();
$cache_id = 'asu_brand:gtm';
return asu_brand_get_cached_content($cache_id, $settings->gtm_path);
}