# AIOZ Stream — Analytics

SDK package: @aiozstream/nodejs-client

## Setup

```typescript
import StreamClient from '@aiozstream/nodejs-client'

const client = new StreamClient({
  publicKey: process.env.AIOZ_PUBLIC_KEY!,
  secretKey: process.env.AIOZ_SECRET_KEY!
})
```

---

## Retrieve Aggregated Metrics

POST https://api.aiozstream.network/api/analytics/metrics/data/{metric}/{aggregation}

Metrics: play | start | end | impression | watch_time | view
Aggregations:
- count: total event count (play, start, end, impression, view)
- rate: plays / impressions ratio (play only)
- total: total count (play only)
- average: average value (watch_time only)
- sum: sum of values (watch_time only)

```typescript
const now = Math.floor(Date.now() / 1000)
const thirtyDaysAgo = now - 30 * 24 * 60 * 60

const result = await client.analytics.GetAggregatedMetrics('play', 'count', {
  from: thirtyDaysAgo,
  to: now
})
console.log(result.data?.data) // number
```

Filter options (pass in the request body):
- media_ids: string[] — filter by specific video/stream IDs
- media_type: 'video' | 'live-stream'
- continents: ('AS'|'AF'|'NA'|'SA'|'AN'|'EU'|'AZ')[]
- countries: string[] — ISO country codes
- device_types: ('computer'|'phone'|'tablet'|'tv'|'console'|'wearable'|'unknown')[]
- os: ('windows'|'mac osx'|'android'|'ios'|'linux')[]
- browsers: ('chrome'|'firefox'|'edge'|'opera')[]
- tags: string[]

```typescript
// With filters
const result = await client.analytics.GetAggregatedMetrics('impression', 'count', {
  from: thirtyDaysAgo,
  to: now,
  filter_by: {
    media_type: 'video',
    countries: ['US', 'GB']
  }
})
```

---

## Retrieve Metrics by Breakdown (Dimension)

POST https://api.aiozstream.network/api/analytics/metrics/bucket/{metric}/{breakdown}

Metrics: play | play_rate | start | end | impression | watch_time | retention | view
Breakdowns: media-id | media-type | continent | country | device-type | operating-system | browser

```typescript
const result = await client.analytics.GetBreakdownMetrics('impression', 'country', {
  from: thirtyDaysAgo,
  to: now,
  limit: 25,
  offset: 0,
  sort_by: 'metric_value',
  order_by: 'desc'
})
// result.data?.data: [{ metric_value, dimension_value, emitted_at }]
// result.data?.total
```

---

## Retrieve Metrics Over Time

POST https://api.aiozstream.network/api/analytics/metrics/timeseries/{metric}/{interval}

Intervals: hour | day

```typescript
const sevenDaysAgo = now - 7 * 24 * 60 * 60

const result = await client.analytics.GetOvertimeMetrics('impression', 'day', {
  from: sevenDaysAgo,
  to: now
})
// result.data?.data: [{ metric_value, emitted_at }]
// result.data?.total
```

---

## Get Data Usage

GET https://api.aiozstream.network/api/analytics/data

```typescript
const result = await client.analytics.GetDataUsage()
// result.data — usage statistics
```

---

## Get Statistic Medias

GET https://api.aiozstream.network/api/analytics/media

```typescript
const result = await client.analytics.GetStatisticMedias()
// result.data — per-media statistics
```
