|
1 | | -# google-analytics |
2 | | -Provides an integration with google analytics |
| 1 | +# Google Analytics Plugin for dotCMS |
| 2 | + |
| 3 | +OSGi plugin that integrates Google Analytics 4 (GA4) Data API with dotCMS, enabling you to fetch analytics data programmatically via Velocity viewtools. |
| 4 | + |
| 5 | +[](https://github.com/dotCMS/google-analytics/releases) |
| 6 | + |
| 7 | +## What It Does |
| 8 | + |
| 9 | +This plugin provides a `$analytics` viewtool in Velocity templates for querying Google Analytics 4 data directly from your dotCMS pages. Retrieve metrics like sessions, active users, page views, and more—filtered by dimensions like date, page path, device category, etc. |
| 10 | + |
| 11 | +**Note:** This plugin *fetches* analytics data from Google Analytics. It does NOT add tracking code to your site. |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | + |
| 17 | +- dotCMS 23.01.10 or higher |
| 18 | +- Google Cloud Platform account with billing enabled |
| 19 | +- Google Analytics 4 property with data to query |
| 20 | +- Service account JSON credentials from Google Cloud |
| 21 | + |
| 22 | +### Installation |
| 23 | + |
| 24 | +1. **Download the latest release** |
| 25 | + - Go to [Releases](https://github.com/dotCMS/google-analytics/releases) |
| 26 | + - Download `google-analytics-X.X.X.jar` |
| 27 | + |
| 28 | +2. **Upload to dotCMS** |
| 29 | + - Log into dotCMS as admin |
| 30 | + - Go to **System → Dynamic Plugins** |
| 31 | + - Click **Upload Plugin** |
| 32 | + - Select the JAR file |
| 33 | + |
| 34 | +3. **Configure the App** |
| 35 | + - Go to **System → Apps → Google Analytics** |
| 36 | + - **Application Name**: Any name (e.g., "My GA4 Property") |
| 37 | + - **Json Key File**: Paste your Google Cloud service account JSON credentials |
| 38 | + - Click **Save** |
| 39 | + |
| 40 | +### Basic Usage |
| 41 | + |
| 42 | +```velocity |
| 43 | +<h2>Last 7 Days Analytics</h2> |
| 44 | +
|
| 45 | +## Your GA4 property ID (just the number) |
| 46 | +#set($propertyId = "488595222") |
| 47 | +
|
| 48 | +## Create and configure request |
| 49 | +#set($gaRequest = $analytics.createAnalyticsRequest($propertyId)) |
| 50 | +$gaRequest.setStartDate("2026-02-09") |
| 51 | +$gaRequest.setEndDate("2026-02-16") |
| 52 | +$gaRequest.setMetrics("sessions,activeUsers") |
| 53 | +$gaRequest.setDimensions("date") |
| 54 | +
|
| 55 | +## Execute query |
| 56 | +#set($gaResponse = $analytics.query($gaRequest)) |
| 57 | +
|
| 58 | +## Display results |
| 59 | +<table> |
| 60 | + <thead> |
| 61 | + <tr><th>Date</th><th>Sessions</th><th>Active Users</th></tr> |
| 62 | + </thead> |
| 63 | + <tbody> |
| 64 | + #foreach($row in $gaResponse.getRowsList()) |
| 65 | + <tr> |
| 66 | + <td>$row.getDimensionValues(0).getValue()</td> |
| 67 | + <td>$row.getMetricValues(0).getValue()</td> |
| 68 | + <td>$row.getMetricValues(1).getValue()</td> |
| 69 | + </tr> |
| 70 | + #end |
| 71 | + </tbody> |
| 72 | +</table> |
| 73 | +``` |
| 74 | + |
| 75 | +## Documentation |
| 76 | + |
| 77 | +For complete setup instructions including Google Cloud configuration, Google Analytics permissions, advanced usage, and troubleshooting: |
| 78 | + |
| 79 | +**📖 [Full Integration Guide](https://www.dotcms.com/integrations/google-analytics)** |
| 80 | + |
| 81 | +### Key Topics Covered |
| 82 | + |
| 83 | +- **Google Cloud Platform Setup** - Creating service accounts and enabling the Analytics Data API |
| 84 | +- **Google Analytics Configuration** - Granting access and finding your property ID |
| 85 | +- **Advanced Queries** - Filters, dimensions, metrics, and data processing |
| 86 | +- **Troubleshooting** - OSGi issues, metric errors, variable name conflicts |
| 87 | +- **Available Metrics & Dimensions** - GA4 API schema reference |
| 88 | + |
| 89 | +## Available Metrics |
| 90 | + |
| 91 | +Common GA4 metrics you can query: |
| 92 | +- `sessions` - Number of sessions |
| 93 | +- `activeUsers` - Number of distinct users |
| 94 | +- `screenPageViews` - Total page and screen views |
| 95 | +- `bounceRate` - Percentage of single-page sessions |
| 96 | +- `averageSessionDuration` - Average session duration in seconds |
| 97 | + |
| 98 | +See the [GA4 API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) for the full list. |
| 99 | + |
| 100 | +## Version History |
| 101 | + |
| 102 | +### 0.4.1 (Current) |
| 103 | +- ✅ GA4 compatibility - Changed default metric from `ga:visits` to `sessions` |
| 104 | +- ✅ Fixed OSGi dependency resolution for local/Docker deployments |
| 105 | +- ✅ Added proper Import-Package whitelist for dotCMS classes |
| 106 | +- ✅ Bundle all Google Analytics Data API dependencies inside plugin JAR |
| 107 | +- Compatible with dotCMS 23.01.10+ |
| 108 | + |
| 109 | +## Building from Source |
| 110 | + |
| 111 | +```bash |
| 112 | +git clone https://github.com/dotCMS/google-analytics.git |
| 113 | +cd google-analytics |
| 114 | +./gradlew jar |
| 115 | +``` |
| 116 | + |
| 117 | +The JAR will be in `build/libs/google-analytics-0.4.1.jar` |
| 118 | + |
| 119 | +## Contributing |
| 120 | + |
| 121 | +Contributions are welcome! Please: |
| 122 | + |
| 123 | +1. Fork the repository |
| 124 | +2. Create a feature branch (`git checkout -b feature/amazing-feature`) |
| 125 | +3. Make your changes |
| 126 | +4. Commit with clear messages |
| 127 | +5. Push to your branch |
| 128 | +6. Open a Pull Request |
| 129 | + |
| 130 | +## Support |
| 131 | + |
| 132 | +- **Issues & Bugs:** [GitHub Issues](https://github.com/dotCMS/google-analytics/issues) |
| 133 | +- **Documentation:** [dotCMS Integration Guide](https://www.dotcms.com/integrations/google-analytics) |
| 134 | +- **dotCMS Docs:** [www.dotcms.com/docs](https://www.dotcms.com/docs) |
| 135 | + |
| 136 | +## License |
| 137 | + |
| 138 | +This plugin is provided as-is by dotCMS. |
0 commit comments