Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Netlify Analytics API requests #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cmake.configureOnOpen": false,
"cSpell.words": [
"Vercel"
]
}
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

<h1 align="center">market-stats</h1>

> Stats endpoint to deliver key numbers to market UI.

[![Vercel deployment](https://flat.badgen.net/badge/vercel/auto-deployment/21c4dd?icon=now)](https://vercel.com/oceanprotocol/market)
[![js oceanprotocol](https://img.shields.io/badge/js-oceanprotocol-7b1173.svg)](https://github.com/oceanprotocol/eslint-config-oceanprotocol)

```ts
interface MarketStatsResponse {
datasets: number // total number of published datasets
owners: number // total number of different owner accounts
ocean: number // total number of pooled OCEAN
datatoken: number // total number of pooled Datatokens
}
```
Market stats provide an API encapsulating the Netlify Analytics API endpoints and give developers secure and out-of-the-box API to use directly into their markets.

For a detailed explanation of how the Netlift Analytics API works, what are the different endpoints, request, and responses, and how to use them visit [netlify-analytics-api-docs](https://github.com/DimitarSD/netlify-analytics-api-docs)

Deployed under https://market-stats.oceanprotocol.com

Expand Down
3 changes: 3 additions & 0 deletions api/helpers/endpoint-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const apiUri = process.env.NETLIFY_ANALYTICS_API_URI
export const apiId = process.env.NETLIFY_ANALYTICS_API_ID
export const token = process.env.NETLIFY_ANALYTICS_API_AUTH_TOKEN
4 changes: 4 additions & 0 deletions api/helpers/resolution-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum RESOLUTION_TYPE {
DAY = 'day',
RANGE = 'range'
}
99 changes: 0 additions & 99 deletions api/index.ts

This file was deleted.

46 changes: 46 additions & 0 deletions api/netlify-analytics/not-found-sources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fetch from 'node-fetch'
import { VercelRequest, VercelResponse } from '@vercel/node'

import { apiUri, apiId, token } from './../helpers/endpoint-helper'
import { validatePeriod, validateLimit } from './../utils/validator'

export default async (
req: VercelRequest,
res: VercelResponse
): Promise<void> => {
try {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'POST')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.setHeader(
'Access-Control-Allow-Headers',
'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
)

const { from, to, timezone, limit } = req.query

validatePeriod(from, to, res)
validateLimit(limit, res)

const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
Authorization: `Bearer ${token}`
}
}

const endpoint = `${apiUri}/${apiId}/ranking/not_found?from=${from}&to=${to}&timezone=${timezone}&limit=${limit}`
const response = await fetch(endpoint, options)

if (!response || !response.ok || response.status !== 200) {
res.status(200).json({ status: 'error' })
return
}

const responseJson = await response.json()
res.status(200).send(responseJson)
} catch (error) {
res.status(200).json({ status: 'error' })
}
}
46 changes: 46 additions & 0 deletions api/netlify-analytics/page-views.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fetch from 'node-fetch'
import { VercelRequest, VercelResponse } from '@vercel/node'

import { apiUri, apiId, token } from './../helpers/endpoint-helper'
import { validatePeriod, validateResolution } from './../utils/validator'

export default async (
req: VercelRequest,
res: VercelResponse
): Promise<void> => {
try {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'POST')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.setHeader(
'Access-Control-Allow-Headers',
'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
)

const { from, to, timezone, resolution } = req.query

validatePeriod(from, to, res)
validateResolution(resolution, res)

const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
Authorization: `Bearer ${token}`
}
}

const endpoint = `${apiUri}/${apiId}/pageviews?from=${from}&to=${to}&timezone=${timezone}&resolution=${resolution}`
const response = await fetch(endpoint, options)

if (!response || !response.ok || response.status !== 200) {
res.status(200).json({ status: 'error' })
return
}

const responseJson = await response.json()
res.status(200).send(responseJson)
} catch (error) {
res.status(200).json({ status: 'error' })
}
}
45 changes: 45 additions & 0 deletions api/netlify-analytics/top-locations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import fetch from 'node-fetch'
import { VercelRequest, VercelResponse } from '@vercel/node'

import { apiUri, apiId, token } from './../helpers/endpoint-helper'
import { validatePeriod } from './../utils/validator'

export default async (
req: VercelRequest,
res: VercelResponse
): Promise<void> => {
try {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'POST')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.setHeader(
'Access-Control-Allow-Headers',
'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
)

const { from, to, timezone } = req.query

validatePeriod(from, to, res)

const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
Authorization: `Bearer ${token}`
}
}

const endpoint = `${apiUri}/${apiId}/ranking/countries?from=${from}&to=${to}&timezone=${timezone}`
const response = await fetch(endpoint, options)

if (!response || !response.ok || response.status !== 200) {
res.status(200).json({ status: 'error' })
return
}

const responseJson = await response.json()
res.status(200).send(responseJson)
} catch (error) {
res.status(200).json({ status: 'error' })
}
}
46 changes: 46 additions & 0 deletions api/netlify-analytics/top-pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fetch from 'node-fetch'
import { VercelRequest, VercelResponse } from '@vercel/node'

import { apiUri, apiId, token } from './../helpers/endpoint-helper'
import { validatePeriod, validateLimit } from './../utils/validator'

export default async (
req: VercelRequest,
res: VercelResponse
): Promise<void> => {
try {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'POST')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.setHeader(
'Access-Control-Allow-Headers',
'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
)

const { from, to, timezone, limit } = req.query

validatePeriod(from, to, res)
validateLimit(limit, res)

const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
Authorization: `Bearer ${token}`
}
}

const endpoint = `${apiUri}/${apiId}/ranking/pages?from=${from}&to=${to}&timezone=${timezone}&limit=${limit}`
const response = await fetch(endpoint, options)

if (!response || !response.ok || response.status !== 200) {
res.status(200).json({ status: 'error' })
return
}

const responseJson = await response.json()
res.status(200).send(responseJson)
} catch (error) {
res.status(200).json({ status: 'error' })
}
}
46 changes: 46 additions & 0 deletions api/netlify-analytics/top-sources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fetch from 'node-fetch'
import { VercelRequest, VercelResponse } from '@vercel/node'

import { apiUri, apiId, token } from './../helpers/endpoint-helper'
import { validatePeriod, validateLimit } from './../utils/validator'

export default async (
req: VercelRequest,
res: VercelResponse
): Promise<void> => {
try {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'POST')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.setHeader(
'Access-Control-Allow-Headers',
'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
)

const { from, to, timezone, limit } = req.query

validatePeriod(from, to, res)
validateLimit(limit, res)

const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
Authorization: `Bearer ${token}`
}
}

const endpoint = `${apiUri}/${apiId}/ranking/sources?from=${from}&to=${to}&timezone=${timezone}&limit=${limit}`
const response = await fetch(endpoint, options)

if (!response || !response.ok || response.status !== 200) {
res.status(200).json({ status: 'error' })
return
}

const responseJson = await response.json()
res.status(200).send(responseJson)
} catch (error) {
res.status(200).json({ status: 'error' })
}
}
Loading