Skip to content

Hooks

hook_analytics_service_access()

Controls access to an analytics service, including whether it tracks.

This is core's hook_ENTITY_TYPE_access() for the analytics_service entity type. The 'track' operation decides whether the service outputs its code on the current request. By default, services don't track admin routes, or users with the 'bypass all analytics services' permission, and disabled services don't track.

Return forbidden to stop the service tracking. Return allowed to track where the plugin is neutral, such as on an admin route. Access results are combined, and any forbidden result wins. Add the cache contexts and tags of anything the result depends on: the result's cacheability is added to the page whether or not it's allowed.


  • Full name: hook_analytics_service_access
  • Defined in: analytics.api.php

See Also:

  • \Drupal\analytics\Plugin\ServicePluginBase::access()
  • \Drupal\analytics\AnalyticsServiceAccessControlHandler

Parameters

Parameter Type Description
$service \Drupal\analytics\Entity\AnalyticsServiceInterface
The analytics service. id() returns the service's ID.
$operation string
The operation, such as 'track'.
$account \Drupal\Core\Session\AccountInterface
The user the access is checked for.

Return Value

\Drupal\Core\Access\AccessResultInterface

The access result. Return neutral for operations the hook doesn't handle.


hook_analytics_SERVICE_ID_data_alter()

Alters the data an analytics service pushes to its data layer.

SERVICE_ID is the analytics service's ID, such as 'gtm' for a service saved at admin/config/services/analytics/gtm. For Google Tag Manager, each key in $data becomes a data layer variable that the container's tags and triggers can use as a "Data Layer Variable", and the data starts as the service's "Data layer value JSON" setting. For GoatCounter, the keys are window.goatcounter settings, such as 'path' or 'title', and the data starts with the options turned on in the service. Values are encoded as JSON, so use strings, numbers, booleans, NULL and arrays.

The data is part of the cached page. Add the cache tags and contexts of anything the values depend on to $cacheability, such as the entity whose fields they come from, or the 'user.roles' context for the user's roles.


  • Full name: hook_analytics_SERVICE_ID_data_alter
  • Defined in: analytics.api.php

See Also:

  • \hook_analytics_PLUGIN_ID_data_alter()
  • \Drupal\analytics\Plugin\ServiceDataTrait::getData()

Parameters

Parameter Type Description
$data array
The data to push to the data layer.
$service \Drupal\analytics\Plugin\ServicePluginInterface
The service's plugin.
$cacheability \Drupal\Core\Cache\RefinableCacheableDependencyInterface
The cacheability of the data, added to the service's output.

Return Value

void


hook_analytics_PLUGIN_ID_data_alter()

Alters the data of every analytics service that uses a plugin.

PLUGIN_ID is the analytics service plugin's ID, such as 'google_tag_manager' or 'goatcounter'. It's invoked together with hook_analytics_SERVICE_ID_data_alter(), only when the plugin ID isn't also the service's ID.


  • Full name: hook_analytics_PLUGIN_ID_data_alter
  • Defined in: analytics.api.php

See Also:

  • \hook_analytics_SERVICE_ID_data_alter()

Parameters

Parameter Type Description
$data array
The data to push to the data layer.
$service \Drupal\analytics\Plugin\ServicePluginInterface
The service's plugin. getServiceId() returns the service's ID.
$cacheability \Drupal\Core\Cache\RefinableCacheableDependencyInterface
The cacheability of the data, added to the service's output.

Return Value

void