Include Google Analytics script in separate file

Created on 23 April 2022, over 2 years ago
Updated 23 May 2023, over 1 year ago

Problem/Motivation

The documentation for this module states that it will "automatically add the required Javascript [tracking code] to every page generated by Drupal." However, content security policies generally do not allow inline scripts to be included. Console errors look something like: "Content Security Policy: The page’s settings blocked the loading of a resource at inline (“script-src”)."

Steps to reproduce

Install the Google Analytics module as described. Ensure that your CSP does not include 'unsafe-inline' for the script-src directive.

Proposed resolution

Following the approach taken to patch the Drupal 7 version of this module ( #3170031 Add option to write script code to file (rather than inline) RTBC ), move the generated script into a separate file.

Remaining tasks

Do it!

User interface changes

Possibly add this feature as an option, rather than as a default implementation change.

API changes

Not anticipating any...

Data model changes

Likely just a single added field to the module configuration.

Feature request
Status

Needs review

Version

4.0

Component

Code

Created by

🇺🇸United States beunerd Providence, Rhode Island

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The patch in #18 seems to be working for me as well. Thanks very much.

    Hopefully this can get picked up, as it seems to be the best way for GA4 on a site with a strong CSP.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    run-tests.sh fatal error
  • After updating to Drupal 10.1, the patch in #18 stopped working for me. When the url was being built, it didn't properly parse out the assets folder, so I ended up with pages linking to src="//js/google-analytics-XX".

    In my case, I don't have an assets folder specified, but with the logic in the patch, it comes back as existing and uses the same value as the public folder. I found that a small change from $settings->get('file_assets_path') to AssetsStream::basePath() made the difference for me.

    Otherwise the rest of the changes from r.van.doorn work great. I'm uploading my patch which is basically all of r.van.doorn's work with two lines changed, as well as an interdiff. This works for me on D10.1, and still seems to work as expected on D9.5 as well.

  • Status changed to Needs work about 1 month ago
  • 🇳🇱Netherlands eelkeblok Netherlands 🇳🇱

    There is a merge request, can that be updated based on the latest changes? Patch workflow is not going to stick around for the long haul, best get to grips with the MR workflow. Also, a quick glance at the patch told me that there are some unrelated code changes in the patch. It is best to limit code changes for an issue to the changes required to solve the issue at hand; it limits the chance of merge conflicts and reduces the number of lines to review.

  • 🇳🇱Netherlands eelkeblok Netherlands 🇳🇱

    I updated the MR with the latest changes from #21 and merged in 4.x in the process. I'm looking into the implications of using the AssetsStream wrapper directly... I don't think this will work in the current form with Drupal 9.3, which is currently the oldest core version this module claims to support.

  • 🇳🇱Netherlands eelkeblok Netherlands 🇳🇱

    Just noticed the deprecated notice on the module status and found more information in the documentation (maybe a bit more information wouldn't hurt on the project page either).

    Anyway, I rolled back some unrelated changes, which hopefully makes reviewing this a little easier. However, with tests out due to the move to GitLab, I have little faith in this actually ever making the finish line...

  • First commit to issue fork.
  • 🇺🇸United States japerry KVUO

    Unfortunately I do not believe such a large change will occur in this module, due to its EOL status. Working to update the documentation so that users will goto the Google Tag module instead. Fortunately the Google Tag module incorporates the goals of this patch, so please try it out!

  • 🇳🇱Netherlands eelkeblok Netherlands 🇳🇱

    Thanks. The customer was already in the process of moving to GTM, so this was a nice extra nudge :)

Production build 0.71.5 2024