<!-- Copyright (c) 2026 defconxt. All rights reserved. -->
<!-- Licensed under AGPL-3.0 — see LICENSE file for details. -->
---
name: implementing-privacy-preserving-analytics
description: >-
  Implement privacy-preserving analytics pipelines using aggregation,
  k-anonymity, local differential privacy, and purpose-limited collection
  to enable data insights without exposing individual records, compliant
  with GDPR Art. 5(1)(c) data minimization.
domain: cybersecurity
subdomain: privacy-engineering
tags:
  - analytics
  - privacy-preserving
  - k-anonymity
  - data-minimization
  - gdpr
  - aggregation
version: "1.0"
author: defconxt
license: AGPL-3.0
metadata:
  mitre-attack: []
  mitre-attack: []
---

# Implementing Privacy-Preserving Analytics

## Overview

Privacy-preserving analytics enables organizations to derive insights from user
data without exposing individual records. Traditional analytics pipelines collect
granular user-level data that creates regulatory and breach risk. This skill
implements k-anonymity checks, aggregation pipelines, local noise injection, and
purpose-limited collection strategies compliant with GDPR Art. 5(1)(c).

Mode: `[MODE: PRIVACY]` — Data minimization in analytics pipelines.

## Prerequisites

- Python 3.10+ with `json`, `random`, `statistics` modules
- Access to analytics data pipeline configuration
- Understanding of current data collection points
- Data flow diagram for analytics processing

## Key Concepts

### Privacy-Preserving Techniques for Analytics

| Technique | Privacy Guarantee | Data Utility | Complexity |
|-----------|------------------|-------------|------------|
| Aggregation | Group-level only | High | Low |
| k-Anonymity | k indistinguishable records | Medium-High | Medium |
| l-Diversity | l distinct sensitive values | Medium | Medium |
| t-Closeness | Distribution similarity | Medium-Low | High |
| Local DP | Plausible deniability per record | Medium | Medium |
| Suppression | Remove low-count groups | High (remaining) | Low |

### k-Anonymity Validation

```python
from collections import Counter

def check_k_anonymity(records, quasi_ids, k):
    groups = Counter(
        tuple(r.get(qi, "") for qi in quasi_ids)
        for r in records
    )
    return all(count >= k for count in groups.values())
```

### Scanning CLI

```bash
# Check k-anonymity of a dataset
node scripts/agent.js --action check-k-anonymity \
  --input /tmp/dataset.json --quasi-ids "age,zipcode,gender" --k 5

# Apply aggregation to analytics data
node scripts/agent.js --action aggregate \
  --input /tmp/user_events.json --group-by "country,device_type" \
  --metrics "count,avg_duration"

# Add local noise to numeric fields
node scripts/agent.js --action add-noise \
  --input /tmp/dataset.json --fields "age,income" --epsilon 1.0

# Suppress small groups below threshold
node scripts/agent.js --action suppress \
  --input /tmp/dataset.json --group-by "zipcode" --min-count 5
```

## Workflow

### Step 1: Assess Current Collection

```bash
node scripts/agent.js --action check-k-anonymity \
  --input /tmp/dataset.json --quasi-ids "age,zipcode,gender" --k 5
```

### Step 2: Apply Aggregation

```bash
node scripts/agent.js --action aggregate \
  --input /tmp/user_events.json --group-by "country,device_type"
```

### Step 3: Add Noise Where Needed

```bash
node scripts/agent.js --action add-noise \
  --input /tmp/dataset.json --fields "age,income" --epsilon 1.0
```

### Step 4: Suppress Small Groups

```bash
node scripts/agent.js --action suppress \
  --input /tmp/dataset.json --group-by "zipcode" --min-count 5
```

## Verification

- [ ] Analytics pipeline does not collect unnecessary PII (Art. 5(1)(c))
- [ ] k-Anonymity threshold met for all quasi-identifier combinations
- [ ] Small groups suppressed to prevent re-identification
- [ ] Aggregation applied before data leaves secure environment
- [ ] Noise parameters provide meaningful privacy guarantee
- [ ] Data retention limited to analytics purpose duration

## References

- [GDPR Article 5(1)(c) — Data Minimization](https://gdpr-info.eu/art-5-gdpr/)
- [k-Anonymity: A Model for Protecting Privacy (Sweeney, 2002)](https://dataprivacylab.org/dataprivacy/projects/kanonymity/)
- [ENISA Data Protection Engineering](https://www.enisa.europa.eu/publications/data-protection-engineering)
- [Article 29 WP Opinion 05/2014 on Anonymisation](https://ec.europa.eu/justice/article-29/documentation/opinion-recommendation/files/2014/wp216_en.pdf)
