Skip to content

Commit f0f56c7

Browse files
committed
track domain specific work
1 parent 48001e0 commit f0f56c7

File tree

3 files changed

+64
-46
lines changed

3 files changed

+64
-46
lines changed

config/schema/cornell_mixpanel.schema.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ cornell_mixpanel.settings:
2323
cornell_mixpanel_cross_subdomain_cookie:
2424
type: boolean
2525
label: 'Cross Subdomain Cookie'
26+
cornell_mixpanel_domains_to_track:
27+
type: string
28+
label: 'Domains to Track'
2629
cornell_mixpanel_track_only_anonymous:
2730
type: boolean
2831
label: 'Track Only Anonymous'

cornell_mixpanel.module

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,67 @@
44
* Implements hook_permission().
55
*/
66
function cornell_mixpanel_permission() {
7-
return [
8-
'administer cornell_mixpanel' => [
9-
'title' => t('Administer Cornell Mixpanel'),
10-
'description' => t('Set Mixpanel token and change other internal settings for Mixpanel integration.'),
11-
],
12-
];
7+
return [
8+
'administer cornell_mixpanel' => [
9+
'title' => t('Administer Cornell Mixpanel'),
10+
'description' => t('Set Mixpanel token and change other internal settings for Mixpanel integration.'),
11+
],
12+
];
1313
}
1414

1515
/**
1616
* Implements hook_help().
1717
*/
1818
function cornell_mixpanel_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
19-
switch ($route_name) {
20-
case 'help.page.cornell_mixpanel':
21-
return '<p>' . t('Provides integration with the Mixpanel real-time analytics service. <strong>For more information, see the <a href="!docs_url" target="_blank">full documentation</a> online.</strong>', [
22-
'!docs_url' => 'https://drupal.org/node/2096053',
23-
]) . '</p>';
24-
}
19+
switch ($route_name) {
20+
case 'help.page.cornell_mixpanel':
21+
return '<p>' . t('Provides integration with the Mixpanel real-time analytics service. <strong>For more information, see the <a href="!docs_url" target="_blank">full documentation</a> online.</strong>', [
22+
'!docs_url' => 'https://drupal.org/node/2096053',
23+
]) . '</p>';
24+
}
2525
}
26-
function cornell_mixpanel_preprocess_page(&$variables)
27-
{
28-
$config = \Drupal::config('cornell_mixpanel.settings');
29-
30-
$user = \Drupal::currentUser();
31-
if ($config->get('cornell_mixpanel_track_only_anonymous') && $user->isAuthenticated()) {
32-
return;
33-
}
34-
35-
$route_name = \Drupal::routeMatch()->getRouteName();
36-
if ($config->get('cornell_mixpanel_ignore_admin_routes') && strpos($route_name, 'admin') !== false) {
37-
return;
38-
}
39-
40-
$mixpanel_config = [];
41-
$proxy_config = $config->get('cornell_mixpanel_prod_proxy_domain');
42-
if($proxy_config && $proxy_config != "") {
43-
$proxy_config = rtrim(trim($proxy_config), '/');
44-
}
45-
else {
46-
$proxy_config = "https://api-js.mixpanel.com";
47-
}
48-
$mixpanel_config['proxy_server'] = $proxy_config;
26+
function cornell_mixpanel_preprocess_page(&$variables) {
27+
$config = \Drupal::config('cornell_mixpanel.settings');
4928

50-
if (isset($_ENV['PANTHEON_ENVIRONMENT']) && $_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
51-
$mixpanel_config['token'] = trim($config->get('cornell_mixpanel_token'));
52-
}
53-
else {
54-
$mixpanel_config['token'] = trim($config->get('cornell_mixpanel_test_token'));
29+
$domains_to_track = $config->get('cornell_mixpanel_domains_to_track');
30+
if ($domains_to_track) {
31+
$domains_to_track = array_map('trim', explode(',', $domains_to_track));
32+
$current_domain = \Drupal::request()->getHost();
33+
if (!in_array($current_domain, $domains_to_track)) {
34+
return;
5535
}
36+
}
37+
38+
$user = \Drupal::currentUser();
39+
if ($config->get('cornell_mixpanel_track_only_anonymous') && $user->isAuthenticated()) {
40+
return;
41+
}
42+
43+
$route_name = \Drupal::routeMatch()->getRouteName();
44+
if ($config->get('cornell_mixpanel_ignore_admin_routes') && strpos($route_name, 'admin') !== FALSE) {
45+
return;
46+
}
47+
48+
$mixpanel_config = [];
49+
$proxy_config = $config->get('cornell_mixpanel_prod_proxy_domain');
50+
if ($proxy_config && $proxy_config != "") {
51+
$proxy_config = rtrim(trim($proxy_config), '/');
52+
}
53+
else {
54+
$proxy_config = "https://api-js.mixpanel.com";
55+
}
56+
$mixpanel_config['proxy_server'] = $proxy_config;
57+
if (isset($_ENV['PANTHEON_ENVIRONMENT']) && $_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
58+
$mixpanel_config['token'] = trim($config->get('cornell_mixpanel_token'));
59+
}
60+
else {
61+
$mixpanel_config['token'] = trim($config->get('cornell_mixpanel_test_token'));
62+
}
63+
64+
$mixpanel_config['cross_subdomain_cookie'] = (bool) $config->get('cornell_mixpanel_cross_subdomain_cookie');
65+
$mixpanel_config['debug_mode'] = (bool) $config->get('cornell_mixpanel_debug_mode');
66+
$mixpanel_config['ignore_dnt'] = (bool) $config->get('cornell_mixpanel_ignore_dnt');
5667

57-
$mixpanel_config['cross_subdomain_cookie'] = (bool) $config->get('cornell_mixpanel_cross_subdomain_cookie');
58-
$mixpanel_config['debug_mode'] = (bool) $config->get('cornell_mixpanel_debug_mode');
59-
$mixpanel_config['ignore_dnt'] = (bool) $config->get('cornell_mixpanel_ignore_dnt');
60-
61-
$variables['#attached']['library'][] = 'cornell_mixpanel/cornell_mixpanel';
62-
$variables['#attached']['drupalSettings']['cornell_mixpanel'] = json_encode($mixpanel_config);
68+
$variables['#attached']['library'][] = 'cornell_mixpanel/cornell_mixpanel';
69+
$variables['#attached']['drupalSettings']['cornell_mixpanel'] = json_encode($mixpanel_config);
6370
}

src/Form/CornellMixpanelAnalyticsSettings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public function buildForm(array $form, FormStateInterface $form_state) {
101101
'#collapsible' => FALSE,
102102
'#collapsed' => FALSE,
103103
];
104+
$form['drupal_specific']['cornell_mixpanel_domains_to_track'] = [
105+
'#type' => 'textarea',
106+
'#title' => $this->t('Domains to track'),
107+
'#description' => $this->t('Enter the domains to track, separated by commas. For example: <em>example.com, sub.example.com</em>. If none are entered, all domains will be tracked.'),
108+
'#rows' => 5,
109+
'#default_value' => $config->get('cornell_mixpanel_domains_to_track'),
110+
];
104111
$form['drupal_specific']['cornell_mixpanel_track_only_anonymous'] = [
105112
'#title' => $this->t('Track only anonymous users'),
106113
'#type' => 'checkbox',
@@ -132,6 +139,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
132139
->set('cornell_mixpanel_cross_subdomain_cookie', $form_state->getValue('cornell_mixpanel_cross_subdomain_cookie'))
133140
->set('cornell_mixpanel_track_only_anonymous', $form_state->getValue('cornell_mixpanel_track_only_anonymous'))
134141
->set('cornell_mixpanel_ignore_admin_routes', $form_state->getValue('cornell_mixpanel_ignore_admin_routes'))
142+
->set('cornell_mixpanel_domains_to_track', $form_state->getValue('cornell_mixpanel_domains_to_track'))
135143
// Save the configuration
136144
->save();
137145

0 commit comments

Comments
 (0)