Implement Google Analytics tracking code injection for sites
Open
@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_ANALYTICSconstant - 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
googleAnalyticsfield 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 constantdotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteHelper.java:328- Field mappingdotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteForm.java:33- Form bindingdotCMS/src/main/java/com/dotcms/rest/api/v1/site/SiteView.java:28- API response
Implementation Options:
- Server-side injection: Intercept HTML output and inject GA script before
</body> - Template-level: Provide Velocity macro/variable for manual placement
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.