dotCMS / dotCMS/core

Implement Google Analytics tracking code injection for sites

Open
#34,665 1 comment 1 reaction 2 assignees View on GitHub

@fmontes is already working on this.

Since Feb 16, 2026.

Team : Scout
Dominant language
Java
Stars
970
Forks
486
Avg merge
3d 33m
Merged PRs (30d)
170

Description

Description

The googleAnalytics field exists in dotCMS sites (Host content type) but is currently only used for storage. The field is exposed via the REST API (SiteResource, SiteForm, SiteView) but has no functional implementation to automatically inject Google Analytics tracking codes into pages.

Current State:

  • Field defined in SiteResource.GOOGLE_ANALYTICS constant
  • Stored and retrieved via REST API endpoints
  • No automatic injection into page templates or HTML output
  • No Velocity/VTL template usage found
  • Users must manually implement GA tracking if they want to use this field

Proposed Implementation:
Automatically inject Google Analytics (GA4) tracking code into pages when the googleAnalytics field is populated on a site.

Acceptance Criteria

  • Read googleAnalytics field value from current site/host context
  • Automatically inject GA4 tracking code into page <head> when field is populated
  • Support both Universal Analytics (UA) and GA4 tracking ID formats
  • Provide configuration option to disable auto-injection if needed
  • Make tracking code available in Velocity context (e.g., $site.googleAnalytics)
  • Update documentation on how to use the Google Analytics field
  • Add integration tests for GA code injection
  • Verify tracking code injection works across different page types (templates, pages, content)

Additional Context

Related Files:

  • dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteResource.java:100 - Field constant
  • dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteHelper.java:328 - Field mapping
  • dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteForm.java:33 - Form binding
  • dotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteView.java:28 - API response

Implementation Options:

  1. Server-side injection: Intercept HTML output and inject GA script before </body>
  2. Template-level: Provide Velocity macro/variable for manual placement
  3. Hybrid: Auto-inject by default with option to use manual placement

GA4 Tracking Code Example:

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXXXXX');
</script>

Considerations:

  • Privacy/GDPR compliance (consider consent management integration)
  • Performance impact of auto-injection
  • Support for Google Tag Manager as alternative
  • Backwards compatibility with existing custom implementations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.