I use the GoogleAnalytics module in most of my projects and have often the same issue:

Due to my countries policy requirements, every user has to be able to deactivate google-analytics cookies. To do so I need a little JavaScript to disable the cookie wich contains the UA/property ID, which is provided by the module.

Normaly such configs are set in the drupal-settings-json at the end of the page but there is no property-id in sight.

<script type="application/json" data-drupal-selector="drupal-settings-json">{"path":{"baseUrl":"\/","scriptPath":null,"pathPrefix":"","current....

is there any way for my JS-function to access this infirmation, or do I need to write an extra function to read the property ID and parse it to the drupal-settings-json somehow?

  • Have a look for the GA script in the page source - does it use a JS variable to pass the ID to the web service, or is it already in a string? If the former, you can use the same var in your JS code; if the latter you'll need to write a bit of PHP code like you thought
    – Clive
    May 31, 2017 at 16:48
  • the module writes the code directly in the head without passing the variable. but I hope there is still a way without writing custom functions. it seems to be a popular usecase (at least every german drupal website using analytics) and a standardized solution seems legit. Jun 1, 2017 at 10:36
  • There isn't a way to do it with writing code. That could be the module developer adding a new feature, of course, but the functionality doesn't exist, so someone needs to code it
    – Clive
    Jun 1, 2017 at 10:40
  • I gues I know who needs to be this someone. tillthen, waiting for further comments / ideas. Jun 1, 2017 at 14:31

1 Answer 1


The most popular Google Analytics module (google_analytics) relies on generating inline JavaScript snippets for initializing tracking, which can make altering its behaviour difficult - likely requiring patching the module.

Googalytics is a more flexible alternative for Google Analytics in Drupal 8

  • drupalSettings is used to pass data to the front end. This removes the use of inline JS, also making the module compitible with Content Security Policy.
    The data structure isn't intended to be modified on the front-end by JS, but hopefully the improved control possible in the back-end makes it unnecessary.
  • There's an open issue (with a patch currently needing feedback) for integrating with the EU Cookie Compliance module. If this matches your use case you may not need to write any custom code, otherwise it may provide a template for how to override the module's JS library for your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.