-
Notifications
You must be signed in to change notification settings - Fork 5
/
gtm.module
175 lines (158 loc) · 4.19 KB
/
gtm.module
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* @file
* Drupal Module: Google Tag Manager
* Provides integration with Google Tag Manager.
*/
/**
* Implements hook_permission().
*/
function gtm_permission() {
return array(
'administer google tag manager' => array(
'title' => t('Administer Google Tag Manager'),
'description' => 'Configure Google Tag Manager.'
),
);
}
/**
* Implements hook_menu().
*/
function gtm_menu() {
$items['admin/settings/gtm'] = array(
'title' => 'Google Tag Manager',
'description' => 'Configure the settings for Google Tag Manager.',
'page callback' => 'drupal_get_form',
'page arguments' => array('gtm_admin_settings_form'),
'access arguments' => array('administer google tag manager'),
'file' => 'gtm.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_theme
*/
function gtm_theme() {
return array(
'gtm_container' => array(
'variables' => array('gtm_id' => NULL),
'file' => 'gtm.theme.inc'
),
'gtm_datalayer' => array(
'variables' => array('dataLayer' => NULL),
'file' => 'gtm.theme.inc'
),
);
}
/**
* Implements hook_page_alter().
*
* Adds a post_render callback and loads the include file.
*
* @see drupal_render_page()
*/
function gtm_page_alter(&$page) {
if (!gtm_should_track()) {
return;
}
// Call sequence:
// - drupal_render_page()
// - hook_page_alter()
// - drupal_render()
// - drupal_render()
// - callbacks in $elements['#theme_wrappers']
// - hook_preprocess_html(): 'html' is the wrapper for page
// - templates may add tags after body tag
// - callbacks in $elements['#post_render']
// - gtm_page_process(): callback set here
// Add callback routine.
$page['#post_render'][] = 'gtm_page_process';
}
/**
* CTools plugin API hook for Context. Note that a proper entry in
* hook_ctools_plugin_api() must exist for this hook to be called.
*/
function gtm_context_plugins() {
$plugins = array();
$plugins['context_reaction_data_layer'] = array(
'handler' => array(
'path' => drupal_get_path('module', 'gtm') .'/plugins',
'file' => 'context_reaction_data_layer.inc',
'class' => 'context_reaction_data_layer',
'parent' => 'context_reaction',
),
);
$plugins['context_reaction_gtm_container'] = array(
'handler' => array(
'path' => drupal_get_path('module', 'gtm') .'/plugins',
'file' => 'context_reaction_gtm_container.inc',
'class' => 'context_reaction_gtm_container',
'parent' => 'context_reaction',
),
);
return $plugins;
}
/**
* Register a reaction
* @return array
*/
function gtm_context_registry() {
return array(
'reactions' => array(
'data_layer' => array(
'title' => t('GTM dataLayer'),
'plugin' => 'context_reaction_data_layer',
),
'gtm_container' => array(
'title' => t('GTM Container'),
'plugin' => 'context_reaction_gtm_container',
)
),
);
}
/**
* Determines if the current role(s) should be tracked
*/
function gtm_should_track() {
global $user;
$id = variable_get('gtm', NULL);
$track = array();
if (is_array($user->roles)) {
foreach ($user->roles as $role) {
$role = str_replace(' ', '_', $role);
$track[] = variable_get('gtm_track_' . $role, FALSE) ? TRUE : FALSE;
}
}
// Don't track page views in the admin sections, or for certain roles.
// Check if we should track the currently active user's role.
if(in_array(TRUE, $track) && arg(0) != 'admin' && !is_null($id)) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_gtm_data_layer
*/
function gtm_gtm_data_layer() {
if($plugin = context_get_plugin('reaction', 'data_layer')) {
return $plugin->execute();
}
return FALSE;
}
/**
* Implements callback_post_render().
*
* Inserts JavaScript snippet immediately following the opening body tag.
*
* @see gtm_page_alter()
* @see drupal_render()
*/
function gtm_page_process(&$children, $elements) {
// Insert snippet after the opening body tag.
if($plugin = context_get_plugin('reaction', 'gtm_container')) {
$script = $plugin->execute();
$children = preg_replace('@<body[^>]*>@', '$0' . $script, $children, 1);
}
return $children;
}