analytics_ext

package
v0.18.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

README

External Analytics Module

A statistics tracking module for oCMS supporting Google Analytics 4 (GA4), Google Tag Manager (GTM), and Matomo.

Features

  • Google Analytics 4 (GA4) integration
  • Google Tag Manager (GTM) integration
  • Matomo (self-hosted analytics) integration
  • Enable/disable each tracker independently
  • Template functions for injecting tracking scripts
  • Admin interface for configuration
  • Settings persisted in database

Supported Platforms

Platform ID Format Example
Google Analytics 4 G-XXXXXXXXXX G-ABC123XYZ
Google Tag Manager GTM-XXXXXXX GTM-ABC1234
Matomo URL + Site ID https://matomo.example.com + 1

Admin Interface

Access at Admin > Modules > External Analytics or /admin/external-analytics.

Routes
Method Path Description
GET /admin/external-analytics Settings dashboard
POST /admin/external-analytics Save settings

Configuration

Google Analytics 4 (GA4)
  1. Navigate to External Analytics settings
  2. Enable GA4
  3. Enter your Measurement ID (G-XXXXXXXXXX)
  4. Save settings

The tracking script is automatically injected into <head> on all public pages.

Google Tag Manager (GTM)
  1. Navigate to External Analytics settings
  2. Enable GTM
  3. Enter your Container ID (GTM-XXXXXXX)
  4. Save settings

GTM script is injected in <head> with noscript fallback at end of <body>.

Note: When GTM is enabled, standalone GA4 script injection is disabled since GA4 is typically configured within GTM.

Matomo
  1. Navigate to External Analytics settings
  2. Enable Matomo
  3. Enter your Matomo server URL (e.g., https://matomo.example.com/)
  4. Enter your Site ID
  5. Save settings

Matomo tracking script is injected in <head> with image tracker fallback for noscript.

Template Functions

Add these functions to your theme's base template:

<!DOCTYPE html>
<html>
<head>
    ...
    {{ analyticsExtHead }}
</head>
<body>
    ...
    {{ analyticsExtBody }}
</body>
</html>
analyticsExtHead

Returns tracking scripts for the <head> section:

  • GTM script tag
  • GA4 gtag.js (when GTM is disabled)
  • Matomo tracking code
analyticsExtBody

Returns tracking scripts for end of <body>:

  • GTM noscript iframe fallback
  • Matomo image tracker fallback

Database Schema

Settings are stored in a single-row table:

CREATE TABLE analytics_settings (
    id INTEGER PRIMARY KEY CHECK (id = 1),
    ga4_enabled INTEGER NOT NULL DEFAULT 0,
    ga4_measurement_id TEXT NOT NULL DEFAULT '',
    gtm_enabled INTEGER NOT NULL DEFAULT 0,
    gtm_container_id TEXT NOT NULL DEFAULT '',
    matomo_enabled INTEGER NOT NULL DEFAULT 0,
    matomo_url TEXT NOT NULL DEFAULT '',
    matomo_site_id TEXT NOT NULL DEFAULT '',
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Module Structure

modules/analytics_ext/
├── module.go      # Module definition, lifecycle, template funcs, migrations
├── handlers.go    # HTTP handlers (dashboard, save settings)
├── settings.go    # Settings struct, load/save, script rendering
└── locales/       # Embedded i18n translations
    ├── en/messages.json
    └── ru/messages.json

Script Injection Details

GTM Head Script
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->
GTM Body Fallback
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
GA4 Script (standalone)
<!-- Google Analytics 4 -->
<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>
<!-- End Google Analytics 4 -->
Matomo Script
<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://matomo.example.com/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

Internationalization

Translations are embedded and automatically loaded. Supported languages:

  • English (en)
  • Russian (ru)

Add new languages by creating locales/{lang}/messages.json.

Module Active Status

The module can be enabled/disabled from Admin > Modules:

  • Active: Routes accessible, template functions inject scripts
  • Inactive: Routes return 404/redirect, no scripts injected

IP Exclusion

External analytics scripts (GA4, GTM, Matomo) are always injected on public pages to ensure accurate pen testing and security audits. To exclude specific IPs from being counted, configure IP filters directly in your analytics platform:

  • Google Analytics 4: Admin > Data Streams > Configure Tag Settings > Define Internal Traffic
  • Google Tag Manager: Use built-in variables to filter by IP in tag triggers
  • Matomo: Administration > Websites > Excluded IPs

For server-side IP exclusion (internal analytics only), use the Internal Analytics module's "Excluded IPs" setting.

Security

  • All IDs are HTML-escaped before injection
  • Settings require admin authentication
  • CSRF protection on form submission
  • Trailing slashes on Matomo URLs are normalized

Validation

When saving settings:

  • GA4 enabled requires Measurement ID
  • GTM enabled requires Container ID
  • Matomo enabled requires both URL and Site ID

Validation errors are displayed as flash messages.

Documentation

Overview

Package analytics_ext provides an external statistics tracking module for oCMS. Supports Google Analytics 4 (GA4), Google Tag Manager (GTM), and Matomo.

templ: version: v0.3.1001

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnalyticsExtPage added in v0.9.0

func AnalyticsExtPage(pc *adminviews.PageContext, data AnalyticsExtViewData) templ.Component

Types

type AnalyticsExtViewData added in v0.9.0

type AnalyticsExtViewData struct {
	GA4Enabled       bool
	GA4MeasurementID string
	GTMEnabled       bool
	GTMContainerID   string
	MatomoEnabled    bool
	MatomoURL        string
	MatomoSiteID     string
}

type Module

type Module struct {
	module.BaseModule
	// contains filtered or unexported fields
}

Module implements the module.Module interface for the external analytics module.

func New

func New() *Module

New creates a new instance of the external analytics module.

func (*Module) AdminURL

func (m *Module) AdminURL() string

AdminURL returns the admin dashboard URL for the module.

func (*Module) Init

func (m *Module) Init(ctx *module.Context) error

Init initializes the module with the given context.

func (*Module) Migrations

func (m *Module) Migrations() []module.Migration

Migrations returns database migrations for the module.

func (*Module) RegisterAdminRoutes

func (m *Module) RegisterAdminRoutes(r chi.Router)

RegisterAdminRoutes registers admin routes for the module.

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(_ chi.Router)

RegisterRoutes registers public routes for the module.

func (*Module) ReloadSettings

func (m *Module) ReloadSettings() error

ReloadSettings reloads settings from the database.

func (*Module) Shutdown

func (m *Module) Shutdown() error

Shutdown performs cleanup when the module is shutting down.

func (*Module) SidebarLabel

func (m *Module) SidebarLabel() string

SidebarLabel returns the display label for the admin sidebar.

func (*Module) TemplateFuncs

func (m *Module) TemplateFuncs() template.FuncMap

TemplateFuncs returns template functions provided by the module.

func (*Module) TranslationsFS

func (m *Module) TranslationsFS() embed.FS

TranslationsFS returns the embedded filesystem containing module translations.

type Settings

type Settings struct {
	GA4Enabled       bool
	GA4MeasurementID string // e.g., "G-XXXXXXXXXX"

	GTMEnabled     bool
	GTMContainerID string // e.g., "GTM-XXXXXXX"

	MatomoEnabled bool
	MatomoURL     string // e.g., "https://matomo.example.com/"
	MatomoSiteID  string // e.g., "1"
}

Settings holds the analytics configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL