|
| 1 | +# Splunk Logging for NHS Notify Web CMS |
| 2 | + |
| 3 | +This document explains how to set up and configure Splunk logging for frontend events in the NHS Notify Web CMS. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Splunk logging system tracks user interactions and navigation events on the NHS Notify website and sends them to Splunk for analysis. Events are sent to the `events_nhsnotify_nonprod` index in non-production environments. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +### Tracked Events |
| 12 | + |
| 13 | +- **Page Views**: When users visit pages |
| 14 | +- **Link Clicks**: When users click on links (internal and external) |
| 15 | +- **Sidebar Navigation**: When users navigate using the sidebar |
| 16 | +- **Search**: When users perform searches (if search functionality exists) |
| 17 | +- **Form Interactions**: When users interact with form fields |
| 18 | +- **Button Clicks**: When users click buttons |
| 19 | +- **File Downloads**: When users download files |
| 20 | +- **External Link Clicks**: When users click links to external sites |
| 21 | + |
| 22 | +### Event Data |
| 23 | + |
| 24 | +Each event includes: |
| 25 | +- Timestamp |
| 26 | +- Session ID (unique per browser session) |
| 27 | +- Environment (development/staging/production) |
| 28 | +- User Agent |
| 29 | +- Page URL and title |
| 30 | +- Referrer |
| 31 | +- Event type, category, action, and label |
| 32 | +- Additional contextual data |
| 33 | + |
| 34 | +## Configuration |
| 35 | + |
| 36 | +### Environment Setup |
| 37 | + |
| 38 | +Edit `docs/_data/splunk-config.yml` to configure Splunk settings for each environment: |
| 39 | + |
| 40 | +```yaml |
| 41 | +environments: |
| 42 | + development: |
| 43 | + enabled: false # Disable in development |
| 44 | + endpoint: "https://dev-splunk-hec.nhs.uk/services/collector" |
| 45 | + token: "your-dev-token" |
| 46 | + index: "events_nhsnotify_nonprod" |
| 47 | + |
| 48 | + staging: |
| 49 | + enabled: true |
| 50 | + endpoint: "https://staging-splunk-hec.nhs.uk/services/collector" |
| 51 | + token: "your-staging-token" |
| 52 | + index: "events_nhsnotify_nonprod" |
| 53 | + |
| 54 | + production: |
| 55 | + enabled: true |
| 56 | + endpoint: "https://prod-splunk-hec.nhs.uk/services/collector" |
| 57 | + token: "your-prod-token" |
| 58 | + index: "events_nhsnotify_prod" |
| 59 | +``` |
| 60 | +
|
| 61 | +### Splunk HTTP Event Collector (HEC) Setup |
| 62 | +
|
| 63 | +1. **Configure HEC in Splunk**: |
| 64 | + - Enable HTTP Event Collector in Splunk |
| 65 | + - Create a new token for the NHS Notify application |
| 66 | + - Configure the index as `events_nhsnotify_nonprod` |
| 67 | + |
| 68 | +2. **Update Configuration**: |
| 69 | + - Replace placeholder endpoints with actual Splunk HEC URLs |
| 70 | + - Replace placeholder tokens with actual HEC tokens |
| 71 | + - Ensure the correct index is specified |
| 72 | + |
| 73 | +## Implementation Details |
| 74 | + |
| 75 | +### Files |
| 76 | + |
| 77 | +- `docs/assets/js/splunk-logger.js`: Main logging implementation |
| 78 | +- `docs/_data/splunk-config.yml`: Configuration settings |
| 79 | +- `docs/_layouts/default.html`: Template integration |
| 80 | + |
| 81 | +### How It Works |
| 82 | + |
| 83 | +1. **Initialization**: The logger initializes when the DOM loads |
| 84 | +2. **Event Listeners**: Automatically attaches listeners for various user interactions |
| 85 | +3. **Data Collection**: Captures relevant event data and page information |
| 86 | +4. **Splunk Format**: Formats events according to Splunk HEC requirements |
| 87 | +5. **HTTP POST**: Sends events to Splunk via HTTP POST requests |
| 88 | + |
| 89 | +### Event Format |
| 90 | + |
| 91 | +Events are sent in Splunk HEC format: |
| 92 | + |
| 93 | +```json |
| 94 | +{ |
| 95 | + "event": { |
| 96 | + "timestamp": "2024-01-15T10:30:00.000Z", |
| 97 | + "session_id": "session_1705312200000_abc123", |
| 98 | + "environment": "staging", |
| 99 | + "user_agent": "Mozilla/5.0...", |
| 100 | + "page_url": "https://notify.nhs.uk/features", |
| 101 | + "page_title": "Features - NHS Notify", |
| 102 | + "page_path": "/features", |
| 103 | + "referrer": "https://notify.nhs.uk/", |
| 104 | + "event_type": "link_click", |
| 105 | + "event_category": "navigation", |
| 106 | + "event_action": "click", |
| 107 | + "event_label": "Emails", |
| 108 | + "event_value": 1, |
| 109 | + "link_url": "https://notify.nhs.uk/features/emails", |
| 110 | + "link_text": "Emails", |
| 111 | + "is_external": false |
| 112 | + }, |
| 113 | + "sourcetype": "nhs_notify_frontend_events", |
| 114 | + "index": "events_nhsnotify_nonprod", |
| 115 | + "host": "notify.nhs.uk" |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Usage Examples |
| 120 | + |
| 121 | +### Manual Event Logging |
| 122 | + |
| 123 | +You can manually log events from JavaScript: |
| 124 | + |
| 125 | +```javascript |
| 126 | +// Log a custom event |
| 127 | +window.splunkLogger.logPageView({ |
| 128 | + custom_data: "additional information" |
| 129 | +}); |
| 130 | +
|
| 131 | +// Log a link click |
| 132 | +window.splunkLogger.logLinkClick(linkElement, { |
| 133 | + custom_context: "sidebar navigation" |
| 134 | +}); |
| 135 | +
|
| 136 | +// Log sidebar navigation |
| 137 | +window.splunkLogger.logSidebarNavigation({ |
| 138 | + section: "Features", |
| 139 | + item: "Emails", |
| 140 | + url: "/features/emails" |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +### Custom Event Tracking |
| 145 | + |
| 146 | +Add custom event tracking for specific interactions: |
| 147 | + |
| 148 | +```javascript |
| 149 | +// Track button clicks |
| 150 | +document.querySelector('.cta-button').addEventListener('click', function() { |
| 151 | + window.splunkLogger.sendToSplunk({ |
| 152 | + type: 'button_click', |
| 153 | + category: 'conversion', |
| 154 | + action: 'click', |
| 155 | + label: 'Get Started Button', |
| 156 | + additionalData: { |
| 157 | + button_text: this.textContent, |
| 158 | + button_location: 'hero_section' |
| 159 | + } |
| 160 | + }); |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +## Splunk Queries |
| 165 | + |
| 166 | +### Common Search Queries |
| 167 | + |
| 168 | +```splunk |
| 169 | +# All frontend events |
| 170 | +index=events_nhsnotify_nonprod sourcetype=nhs_notify_frontend_events |
| 171 | +
|
| 172 | +# Page views by section |
| 173 | +index=events_nhsnotify_nonprod sourcetype=nhs_notify_frontend_events event_type=page_view | stats count by page_section |
| 174 | +
|
| 175 | +# Link clicks by destination |
| 176 | +index=events_nhsnotify_nonprod sourcetype=nhs_notify_frontend_events event_type=link_click | stats count by link_url |
| 177 | +
|
| 178 | +# Sidebar navigation usage |
| 179 | +index=events_nhsnotify_nonprod sourcetype=nhs_notify_frontend_events event_type=sidebar_navigation | stats count by sidebar_section, sidebar_item |
| 180 | +
|
| 181 | +# User journey analysis |
| 182 | +index=events_nhsnotify_nonprod sourcetype=nhs_notify_frontend_events | transaction session_id | stats count by page_path |
| 183 | +``` |
| 184 | + |
| 185 | +## Security Considerations |
| 186 | + |
| 187 | +1. **Token Security**: Keep HEC tokens secure and rotate regularly |
| 188 | +2. **Data Privacy**: Ensure no PII is logged in events |
| 189 | +3. **CORS**: Configure CORS settings in Splunk to allow requests from your domain |
| 190 | +4. **Rate Limiting**: Implement rate limiting to prevent abuse |
| 191 | + |
| 192 | +## Troubleshooting |
| 193 | + |
| 194 | +### Common Issues |
| 195 | + |
| 196 | +1. **Events not appearing in Splunk**: |
| 197 | + - Check HEC endpoint and token |
| 198 | + - Verify index exists and is accessible |
| 199 | + - Check browser console for errors |
| 200 | + |
| 201 | +2. **CORS errors**: |
| 202 | + - Configure CORS settings in Splunk |
| 203 | + - Ensure domain is whitelisted |
| 204 | + |
| 205 | +3. **Events not being sent**: |
| 206 | + - Check if logging is enabled for the environment |
| 207 | + - Verify JavaScript is loading correctly |
| 208 | + - Check network tab for failed requests |
| 209 | + |
| 210 | +### Debug Mode |
| 211 | + |
| 212 | +Enable debug logging by adding to browser console: |
| 213 | + |
| 214 | +```javascript |
| 215 | +// Enable debug mode |
| 216 | +window.splunkLogger.debug = true; |
| 217 | +``` |
| 218 | + |
| 219 | +## Development |
| 220 | + |
| 221 | +### Local Development |
| 222 | + |
| 223 | +For local development, logging is disabled by default. To enable: |
| 224 | + |
| 225 | +1. Update `docs/_data/splunk-config.yml` |
| 226 | +2. Set `enabled: true` for development environment |
| 227 | +3. Configure a test HEC endpoint |
| 228 | + |
| 229 | +### Testing |
| 230 | + |
| 231 | +Test the logging implementation: |
| 232 | + |
| 233 | +1. Open browser developer tools |
| 234 | +2. Navigate to different pages |
| 235 | +3. Click links and interact with forms |
| 236 | +4. Check network tab for requests to Splunk endpoint |
| 237 | +5. Verify events appear in Splunk |
| 238 | + |
| 239 | +## Support |
| 240 | + |
| 241 | +For issues with Splunk logging: |
| 242 | +1. Check browser console for JavaScript errors |
| 243 | +2. Verify Splunk configuration |
| 244 | +3. Test with a simple event first |
| 245 | +4. Contact the development team for assistance |
0 commit comments