CKAN Portal Metrics Plugin A CKAN extension for exporting Google Analytics 4 (GA4) metrics and storing them in CKAN’s Datastore.
- Service Account Creation and Permission Granting
- Configuration
- How Configuration and Credentials Are Handled
- How the CLI Command Works
- Example Usage
- Best Practices
To allow this plugin to access your Google Analytics 4 (GA4) data, you must create a Google Cloud service account and grant it the correct permissions.
- Go to the Google Cloud Console.
- Select or create a project.
- Navigate to IAM & Admin → Service Accounts.
- Click Create Service Account.
- Enter a name and description, then click Create and Continue.
- Assign the role Viewer (or a more restrictive custom role if desired).
- Click Done.
- In the Service Accounts list, click on your new service account.
- Go to the Keys tab.
- Click Add Key → Create new key.
- Choose JSON and click Create.
- Download the JSON file.
Important: Keep this file secure. You will need its contents for your CKAN config.
- Go to Google Analytics Admin.
- Select your GA4 property.
- Under Property, click Account Access Management or Property Access Management.
- Click the + button and choose Add users.
- Enter the service account email (from your JSON key).
- Assign at least the Viewer role (or higher as needed).
- Click Add.
Copy the relevant fields from your JSON key into your ckan.ini as described above:
ckanext.portal_metrics.ga4_client_emailckanext.portal_metrics.ga4_private_key(escape newlines as\\n)ckanext.portal_metrics.ga4_token_uri
Your service account is now ready to be used by the CKAN Portal Metrics plugin.
Add the following to your ckan.ini (or equivalent config):
ckan.plugins = ... portal_metrics
# Required: Google Analytics 4 service account credentials
ckanext.portal_metrics.ga4_client_email = your-service-account@project.iam.gserviceaccount.com
ckanext.portal_metrics.ga4_private_key = -----BEGIN PRIVATE KEY-----\\nMIIEv...IDAQAB\\n-----END PRIVATE KEY-----\\n
ckanext.portal_metrics.ga4_token_uri = https://oauth2.googleapis.com/token
# Required: Your GA4 property ID
ckanext.portal_metrics.ga4_property_id = 123456789
# No trailing slash!
ckan.site_url = https://your-portal.example.com- The plugin implements
IConfigurableand reads all required config values at startup. - It validates that all required values are present and raises an error if any are missing.
- Credentials and settings (
ga4_credentials,property_id,hostname) are stored as class attributes on the plugin (MetricsCliPlugin). - These attributes are then accessed by the CLI command and pipeline code.
- The CLI group and command are registered via the plugin using
IClick. - When you run
ckan metrics fetch, the CLI:- Gets the sysadmin user context.
- Reads credentials, property ID, and hostname from the plugin’s class attributes.
- Passes these to the
MetricsPipeline, which runs the full ETL process.
Example usage:
ckan metrics fetch --lookback-days=7- All configuration is managed via CKAN config (
ckan.ini). - Credentials are loaded and validated once at startup.
- No secrets are hardcoded or loaded from files at runtime.
- The CLI and pipeline access config/credentials via the plugin’s class attributes.