Skip to content

Commit

Permalink
Make the number of website visitors by referrer
Browse files Browse the repository at this point in the history
Refs #77
  • Loading branch information
thewilkybarkid committed Jul 30, 2024
1 parent 636a356 commit 64b7cea
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/data/visitors-by-referrer.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { HttpClient, HttpClientRequest, HttpClientResponse, Terminal } from '@effect/platform'
import { NodeTerminal } from '@effect/platform-node'
import { Schema } from '@effect/schema'
import { Array, Config, Effect, Option, Redacted } from 'effect'

Check failure on line 4 in src/data/visitors-by-referrer.json.ts

View workflow job for this annotation

GitHub Actions / Lint

'Option' is defined but never used
import * as Iso3166 from '../lib/Iso3166.js'

Check failure on line 5 in src/data/visitors-by-referrer.json.ts

View workflow job for this annotation

GitHub Actions / Lint

'Iso3166' is defined but never used
import * as Temporal from '../lib/Temporal.js'
import * as Url from '../lib/Url.js'

const Visitors = Schema.Array(
Schema.Struct({
visits: Schema.NumberFromString,
date: Temporal.PlainYearMonthFromStringSchema,
referrer_hostname: Schema.OptionFromNullOr(Url.UrlFromStringSchema),
}),
)

const Output = Schema.Array(
Schema.Struct({
number: Schema.NumberFromString,
yearMonth: Temporal.PlainYearMonthFromStringSchema,
referrer: Schema.OptionFromNullOr(Url.UrlFromStringSchema),
}),
)

const program = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
const token = yield* Config.redacted('FATHOM_TOKEN')

const request = HttpClientRequest.bearerToken(
HttpClientRequest.get('https://api.usefathom.com/v1/aggregations', {
urlParams: {
entity: 'pageview',
entity_id: 'FEAJEBBA',
aggregates: 'visits',
date_grouping: 'month',
field_grouping: 'referrer_hostname',
},
}),
Redacted.value(token),
)

const data = yield* HttpClient.fetchOk(request).pipe(
Effect.andThen(HttpClientResponse.schemaBodyJson(Visitors)),
Effect.scoped,
)

const transformedData = Array.map(
Array.filter(data, visitors => visitors.visits > 0),
visitors => ({
number: visitors.visits,
yearMonth: visitors.date,
referrer: visitors.referrer_hostname,
}),
)

const encoded = yield* Schema.encode(Schema.parseJson(Output))(transformedData)

yield* terminal.display(encoded)
})

await Effect.runPromise(program.pipe(Effect.provide(NodeTerminal.layer)))

0 comments on commit 64b7cea

Please sign in to comment.