View source
<?php
use Drupal\Core\Routing\RouteMatchInterface;
function google_optimize_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'admin.config.system.google_optimize':
return t('The <a href="@opt_url">Optimize page-hiding snippet</a> supports loading
your Optimize container asynchronously while hiding the page until the container is ready,
ensuring that users don\'t see the initial page content prior to it being modified by an experiment.', [
'@opt_url' => 'https://developers.google.com/optimize/#the_page-hiding_snippet_code',
]);
}
return t('');
}
function google_optimize_hide_page_enabled() {
$config = \Drupal::config('google_optimize.settings');
return (bool) $config
->get('hide_page_enable') ?: FALSE;
}
function google_optimize_container_ids() {
static $ids;
if (!is_null($ids)) {
return $ids;
}
$config = \Drupal::config('google_optimize.settings');
$csv = $config
->get('container_ids') ?: '';
if (empty($csv)) {
$ids = [];
}
else {
$ids = str_getcsv($csv);
}
return $ids;
}
function google_optimize_hide_page_timeout() {
$config = \Drupal::config('google_optimize.settings');
return (int) $config
->get('hide_page_timeout') ?: 4000;
}
function google_optimize_hide_page_class_name() {
$config = \Drupal::config('google_optimize.settings');
return $config
->get('hide_page_class_name') ?: 'async-hide';
}
function google_optimize_hide_page_pages() {
$config = \Drupal::config('google_optimize.settings');
return $config
->get('hide_page_pages') ?: '';
}
function google_optimize_hide_page_roles() {
$config = \Drupal::config('google_optimize.settings');
$roles = $config
->get('hide_page_roles') ?: [];
foreach ($roles as $key => $value) {
if (!$value) {
unset($roles[$key]);
}
}
return $roles;
}
function google_optimize_hide_page_active() {
if (!google_optimize_hide_page_enabled()) {
return FALSE;
}
$admin_context = \Drupal::service('router.admin_context');
if ($admin_context
->isAdminRoute()) {
return FALSE;
}
$container_ids = google_optimize_container_ids();
if (empty($container_ids)) {
return FALSE;
}
if ($pages = google_optimize_hide_page_pages()) {
$current_path = \Drupal::service('path.current')
->getPath();
if (strpos($current_path, '/node/') !== FALSE) {
$current_path = \Drupal::service('path_alias.manager')
->getAliasByPath($current_path);
}
if (!($match = \Drupal::service('path.matcher')
->matchPath($current_path, $pages))) {
return FALSE;
}
}
if ($roles = google_optimize_hide_page_roles()) {
$current_user = \Drupal::currentUser();
$user_roles = $current_user
->getRoles();
foreach ($user_roles as $role) {
if (in_array($role, $roles, TRUE)) {
return TRUE;
}
}
return FALSE;
}
return TRUE;
}
function google_optimize_page_attachments(array &$attachments) {
if (!google_optimize_hide_page_active()) {
return;
}
$container_ids = google_optimize_container_ids();
$container_ids = array_map(function ($value) {
return "'" . trim($value) . "':true";
}, $container_ids);
$container_str = implode(',', $container_ids);
$class_name = google_optimize_hide_page_class_name();
$timeout = google_optimize_hide_page_timeout();
$js = sprintf("(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;\nh.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};\n(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;\n})(window,document.documentElement,'%s','dataLayer',%d,\n{%s});", $class_name, $timeout, $container_str);
foreach ($attachments['#attached']['html_head'] as $key => $tag) {
if (count($tag) && $tag[1] == 'google_analytics_tracking_script') {
array_splice($attachments['#attached']['html_head'], $key, 0, [
[
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $js,
'#attributes' => [],
],
'google-optimize-hide-page-js',
],
]);
break;
}
}
$css = '.' . $class_name . ' { opacity: 0 !important} ';
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'style',
'#value' => $css,
],
'google-optimize-hide-page-css',
];
}
function google_optimize_module_implements_alter(&$implementations, $hook) {
if ($hook == 'page_attachments') {
$group = $implementations['google_optimize'];
unset($implementations['google_optimize']);
$implementations['google_optimize'] = $group;
}
}