-
Notifications
You must be signed in to change notification settings - Fork 1
/
lytics.module
208 lines (182 loc) · 6.21 KB
/
lytics.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
use Drupal\lytics\Entity\LyticsWidget;
/**
* Implements hook_page_attachments_alter().
*/
function lytics_page_attachments_alter(array &$attachments)
{
$config = Drupal::config('lytics.settings');
$attachments['#attached']['drupalSettings']['lytics'] = [
'account_id' => $config->get('account_id'),
];
}
/**
* Implements hook_page_attachments().
*/
function lytics_page_attachments(array &$attachments)
{
$is_admin_route = Drupal::service('router.admin_context')->isAdminRoute();
if (!$is_admin_route) {
$user = Drupal::currentUser();
$config = Drupal::config('lytics.settings');
// If explicitly preventing admin users from being tracked, return early
if ($config->get('ignore_admin_users') && $user->hasPermission('manage lytics connection')) {
return;
}
if ($config->get('enable_tag')) {
// Fetch the tag_config JSON string and decode it to a PHP array
$tagConfigJson = $config->get('tag_config') ?: '{}';
$tagConfigArray = json_decode($tagConfigJson, TRUE);
// Add the 'src' key to the array
$account_id = $config->get('account_id');
$debug_mode = $config->get('debug_mode');
$js_file = $debug_mode ? 'latest.js' : 'latest.min.js';
$tagConfigArray['src'] = 'https://c.lytics.io/api/tag/' . $account_id . '/' . $js_file;
// Re-encode the array back to a JSON string
$modifiedTagConfigJson = json_encode($tagConfigArray);
// Render any published Lytics widgets
$widgets = LyticsWidget::loadMultiple();
function convertJsonToJs($widget)
{
$rawConfig = $widget->getConfiguration();
$cfgObj = json_decode($rawConfig);
$details = $cfgObj->details;
$payload = $cfgObj->config;
$type = ucfirst($details->type);
// special handler for recommendation since they are messages
if ($type === 'Recommendation') {
$type = 'Message';
}
// generate output
$variable = '_pfacfg_' . $payload->id;
$output = 'var ' . $variable . ' = {};' . "\n";;
foreach ($payload as $key => $value) {
if (is_string($value)) {
if ($key === "onInit" || $key === "onLoad") {
if ($value !== "") {
$output .= $variable . '.' . $key . ' = ' . $value . ';' . "\n";
}
} else {
$output .= $variable . '.' . $key . ' = "' . $value . '";' . "\n";;
}
} else {
if ($key === "confirmAction" || $key === "cancelAction" || $key === "closeAction") {
if ($value->callback !== "") {
$output .= $variable . '.' . $key . ' = {
"name": "' . $value->name . '",
"callback": ' . $value->callback . '
}' . "\n";
}
} else {
$output .= $variable . '.' . $key . ' = ' . json_encode($value) . ';' . "\n";
}
}
}
$output .= '__ly_modules.push({
"segment": "' . $details->audience . '",
"widget": new pathfora.' . $type . '(_pfacfg_' . $payload->id . ')
});' . "\n";;
return $output;
}
$outputJS = 'var __ly_modules = [];';
foreach ($widgets as $widget) {
// if widget is not published then skip
$status = $widget->getStatus();
if ($status !== 'published') {
continue;
}
$js = convertJsonToJs($widget);
$outputJS .= $js;
}
// Render the actual Lytics JavaScript SDK
$attachments['#attached']['html_head'][] = [
[
'#type' => 'inline_template',
'#template' => "{% include '@lytics/lytics-install.html.twig' with {'config': config|raw} %}",
'#context' => ['config' => $modifiedTagConfigJson],
],
'lytics_install',
];
$attachments['#attached']['html_head'][] = [
[
'#type' => 'inline_template',
'#template' => '
<script>
var __ly_evaluate_widgets = function(entity){
var segmentMembership = entity?.data?.user?.segments || [];
' . $outputJS . '
__ly_render_widgets = [];
__ly_modules.forEach(function(module){
if(segmentMembership.includes(module.segment) || module.segment === ""){
__ly_render_widgets.push(module.widget);
}
});
pathfora.initializeWidgets(__ly_render_widgets);
};
jstag.on("pathfora.publish.done", function(){
jstag.call("entityReady", __ly_evaluate_widgets);
});
</script>
',
],
'lytics_hello_world',
];
} else {
Drupal::logger('lytics')->notice('Lytics tag is disabled.');
}
}
}
/**
* Implements hook_help().
*
* Displays help and module information.
*
* @param string $route_name
* The route name of the current page.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match interface.
*
* @return string
* A localized string containing the help text.
*/
function lytics_help($route_name, $route_match)
{
switch ($route_name) {
case 'help.page.lytics':
return '<h2>About</h2>'
. '<p>Provides an overview and integration of analytics functionalities.</p>'
. '<h2>Usage</h2>'
. '<p>To configure the Lytics module, navigate to the settings form at Administration > Configuration > System > Lytics settings. Here you can enable the integration, enter your unique Lytics tag ID, and toggle debug mode.</p>';
}
}
/**
* Implements hook_preprocess_HOOK() for page templates.
*/
function lytics_preprocess_page(&$variables)
{
// Example of how to add a CSS file from the module.
$variables['#attached']['library'][] = 'lytics/global-styling';
}
/**
* Implements hook_theme().
*/
function lytics_theme()
{
return [
'todo_item' => [
'variables' => [
'content' => NULL,
],
],
'lytics_install' => [
'variables' => [
'config' => NULL,
],
],
'lytics_widgets' => [
'variables' => [
'config' => NULL,
],
],
];
}