-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgdx_analytics_drupal_snowplow.module
161 lines (134 loc) · 5.88 KB
/
gdx_analytics_drupal_snowplow.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
<?php
/**
* @file
* Contains gdx_analytics_drupal_snowplow.module.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
// Define constants for route names.
define('GDX_ANALYTICS_SETTINGS_FORM_ROUTE', 'gdx_analytics_drupal_snowplow.gdx_analytics_drupal_snowplow_settings_form');
define('GDX_ANALYTICS_HELP_PAGE_ROUTE', 'help.page.gdx_analytics_drupal_snowplow');
// Define constant for tracker versions.
define('GDX_ANALYTICS_WEBTRACKER', 'gdx_analytics_drupal_snowplow/gdx_analytics_drupal_snowplow.webtracker');
define('GDX_ANALYTICS_WEBTRACKER_WITH_SEARCH', 'gdx_analytics_drupal_snowplow/gdx_analytics_drupal_snowplow.webtracker_search');
// Define constant for configuration name.
define('GDX_ANALYTICS_CONFIG_NAME', 'gdx_analytics_drupal_snowplow.settings');
// Define constant for warning message about incomplete configuration.
define('GDX_ANALYTICS_WARNING_CONFIG', t('Please Configure Your GDX Analytics Drupal Snowplow Module.<br>For assistance with filling out this form, contact the GDX Analytics Team.'));
// Define constant for warning message about configuring the module.
define('GDX_ANALYTICS_WARNING_CONFIG_LINK', 'Please click here to configure your GDX Analytics Drupal Snowplow module.');
// Define constant for the module description.
define('GDX_ANALYTICS_MODULE_DESCRIPTION', t('This is the GDX Analytics Drupal Snowplow module.'));
/**
* Implements hook_help().
*/
function gdx_analytics_drupal_snowplow_help($route_name) {
// Help text for the module. $route_name is as part of Drupal routing system
if ($route_name === GDX_ANALYTICS_HELP_PAGE_ROUTE) {
return '<h3>' . t('About') . '</h3><p>' . GDX_ANALYTICS_MODULE_DESCRIPTION . '</p>';
}
}
/**
* Implements hook_page_attachments().
*/
function gdx_analytics_drupal_snowplow_page_attachments(array &$page) {
// Initialize variables.
$admin_context = \Drupal::service('router.admin_context');
$messenger = \Drupal::messenger();
$logger = \Drupal::logger('gdx_analytics_drupal_snowplow');
// Check if the current route is an administrative route.
if ($admin_context->isAdminRoute()) {
// Handle administrative routes.
handleAdminRoutes($logger, $messenger);
} else {
// Handle non-administrative routes.
handleNonAdminRoutes($page, $logger);
}
}
/**
* Handle logic for administrative routes.
*/
function handleAdminRoutes($logger, $messenger) {
// Create link for configuration.
$link = Link::fromTextAndUrl(GDX_ANALYTICS_WARNING_CONFIG_LINK, Url::fromRoute(GDX_ANALYTICS_SETTINGS_FORM_ROUTE));
// Check if configuration is incomplete and display appropriate message.
if (isConfigurationIncomplete($logger, $messenger)) {
if ($route_name === GDX_ANALYTICS_SETTINGS_FORM_ROUTE) {
$messenger->addWarning(GDX_ANALYTICS_WARNING_CONFIG);
} else {
$messenger->addWarning($link);
}
}
}
/**
* Handle logic for non-administrative routes.
*/
function handleNonAdminRoutes(&$page, $logger) {
try {
// Get configuration settings.
$settings = \Drupal::config(GDX_ANALYTICS_CONFIG_NAME)->getRawData();
// Set collector, script uri, app_id, snowplow_version and search path.
$page['#attached']['drupalSettings'] = [
'gdx_collector' => $settings['gdx_collector_mode'],
'app_id' => $settings['gdx_analytics_app_id'],
'snowplow_version' => $settings['gdx_analytics_snowplow_version'],
'search_path' => $settings['gdx_analytics_search_path'],
];
// Attach the main tracking code url.
$page['#attached']['drupalSettings']['script_uri'] = $settings['gdx_analytics_snowplow_script_uri'];
// Handle version-specific logic.
if ($settings['gdx_analytics_snowplow_version'] == 1) {
// Handle Web tracker WITH search function
handleTrackerWithSearch($page, $settings);
} elseif ($settings['gdx_analytics_snowplow_version'] == 0) {
// Handle Web tracker WITHOUT search function
$page['#attached']['library'][] = GDX_ANALYTICS_WEBTRACKER;
}
} catch (\Exception $e) {
// Log the exception and display a message
$logger->error('Error processing non-admin routes: @message', ['@message' => $e->getMessage()]);
}
}
/**
* Handle Web tracker WITH search function
*/
function handleTrackerWithSearch(&$page, $settings) {
// Get the current requested URI
$curr_uri = \Drupal::request()->getRequestUri();
// Retreive the comma separated search path(s) from the settings
$allowed_paths = explode(',', $settings['gdx_analytics_search_path']);
// Check if any of the allowed search paths are a substring of the current URI.
$is_search_page = !empty(array_filter($allowed_paths, function ($allowed_path) use ($curr_uri) {
return strpos($curr_uri, trim($allowed_path)) !== false;
}));
// Check if any of the search paths are in the URL
if ($is_search_page) {
// Extract the search query parameters from the URI (value of the first key)
$search_terms = reset($_GET);
if (!empty($search_terms)) {
$page['#attached']['drupalSettings']['search'] = true;
// Assign the array of individual search terms (use space as delimiter)
$page['#attached']['drupalSettings']['search_terms'] = explode(' ', $search_terms);
}
}
// Attach the tracking code to front-end pages if the search toggle is enabled
$page['#attached']['library'][] = GDX_ANALYTICS_WEBTRACKER_WITH_SEARCH;
}
/**
* Check if configuration settings are incomplete.
*/
function isConfigurationIncomplete($logger, $messenger) {
try {
// Get configuration settings.
$config = \Drupal::config(GDX_ANALYTICS_CONFIG_NAME);
// Retrieve raw data.
$settings = $config->getRawData();
// Check if any setting is empty.
return in_array('', $settings);
} catch (\Exception $e) {
// Log the exception and display a message
$logger->error('Error retrieving configuration: @message', ['@message' => $e->getMessage()]);
$messenger->addError(t('An error occurred while processing the configuration.'));
return false;
}
}