Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor how GA shared ID is added to the pages #787

Open
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions themes/stanford_basic/stanford_basic.theme
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,58 @@ function stanford_basic_page_attachments_alter(array &$attachments) {
}
// Check if dropdown menus are activated
$attachments['#attached']['drupalSettings']['stanford_basic']['nav_dropdown_enabled'] = (bool) theme_get_setting('nav_dropdown_enabled', 'stanford_basic');

// Only add Google Analytics for anonymous users or users without editing
// access while on Acquia environment. Being able to view the toolbar is a
// good indicator that they can edit some part of the site.
Comment on lines +121 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pookmish thanks for these changes.
@buttonwillowsix, does this logic match how we are using our GA tracking code?

Would this opt-out most users on the intranet sites?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (
isset($_ENV['AH_SITE_ENVIRONMENT']) &&
!\Drupal::currentUser()->hasPermission('access toolbar')
) {
_stanford_basic_add_ga_tracking($attachments);
}
}

/**
* Add the necessary GA tracking for shared analytics.
*
* @param array $attachments
* Page attachments from hook_page_attachments_alter().
*/
function _stanford_basic_add_ga_tracking(array &$attachments) {
$add_ga_file = FALSE;
// Look to see if there's already a Google Analytics tracking file included.
if (isset($attachments['#attached']['html_head'])) {
foreach ($attachments['#attached']['html_head'] as $html_head) {
if ($html_head[1] == 'google_analytics_tracking_file') {
$add_ga_file = TRUE;
break;
}
}
}

// No GA tracking file provided by GA module, we'll add our own.
if (!$add_ga_file) {
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'async' => TRUE,
'src' => '//www.googletagmanager.com/gtag/js?id=G-BECJQXLNCY',
],
],
'stanford_basic_analytics_tracking_file',
];
}

// Add the shared GA tracking tag.
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => 'gtag("config", "G-BECJQXLNCY");',
],
'stanford_basic_analytics_tracking',
];
}

/**
Expand Down Expand Up @@ -159,15 +211,6 @@ function stanford_basic_preprocess_html(&$variables) {

// The base path.
$variables['base_path'] = base_path();

// Add global google analytics tracker if the site is on Acquia.
if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
$variables['add_global_ga'] = TRUE;
if (\Drupal::moduleHandler()->moduleExists('google_analytics')) {
$variables['ga_module_enabled'] = !empty(\Drupal::config('google_analytics.settings')
->get('account'));
}
}
}

/**
Expand Down
18 changes: 0 additions & 18 deletions themes/stanford_basic/templates/html.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@
<title>{{ head_title|safe_join(' | ') }}</title>
<css-placeholder token="{{ placeholder_token }}">
<js-placeholder token="{{ placeholder_token }}">

{% if add_global_ga %}
{% if not ga_module_enabled %}
<!-- Global analytics tag (gtag.js) - Google Analytics -->
<script async src="//www.googletagmanager.com/gtag/js?id=G-BECJQXLNCY"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-BECJQXLNCY');
</script>
{% else %}
<script>
gtag('config', 'G-BECJQXLNCY');
</script>
{% endif %}
{% endif %}
</head>
{% set classes = [is_front ? 'front', not is_front ? 'not-front'] %}
{% for role in user.roles %}
Expand Down
Loading