Skip to content

Commit

Permalink
Added regional statistics, removed DNT.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed May 24, 2024
1 parent 2bb2612 commit edadfe8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 63 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 2.7.0

* added regional statistics
* removed DNT
* updated dependencies

## 2.6.0
Expand Down
1 change: 0 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class PirschNodeApiClient extends PirschCoreClient {
const element: PirschHit = {
url: url.toString(),
ip: request.socket.remoteAddress ?? "",
dnt: this.getHeader(request.headers, "dnt"),
user_agent: this.getHeader(request.headers, "user-agent") ?? "",
accept_language: this.getHeader(request.headers, "accept-language"),
sec_ch_ua: this.getHeader(request.headers, "Sec-CH-UA"),
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum PirschEndpoint {
BROWSER = "statistics/browser",
BROWSER_VERSION = "statistics/browser/version",
COUNTRY = "statistics/country",
REGION = "statistics/region",
CITY = "statistics/city",
PLATFORM = "statistics/platform",
SCREEN = "statistics/screen",
Expand Down
49 changes: 16 additions & 33 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
PirschBatchSession,
PirschBatchEvent,
TagStats,
PirschRegionStats,
} from "./types";

import { PIRSCH_DEFAULT_BASE_URL, PIRSCH_DEFAULT_TIMEOUT, PirschEndpoint } from "./constants";
Expand Down Expand Up @@ -112,10 +113,6 @@ export abstract class PirschCoreClient extends PirschCommon {
* @returns APIError or an empty promise, in case something went wrong
*/
async hit(hit: PirschHit): Promise<Optional<PirschApiError>> {
if (hit.dnt === "1") {
return;
}

return await this.performPost(PirschEndpoint.HIT, hit);
}

Expand All @@ -126,13 +123,7 @@ export abstract class PirschCoreClient extends PirschCommon {
* @returns APIError or an empty promise, in case something went wrong
*/
async batchHits(hits: PirschBatchHit[]): Promise<Optional<PirschApiError>> {
const filtered = hits.filter(hit => hit.dnt !== "1");

if (filtered.length === 0) {
return;
}

return await this.performPost(PirschEndpoint.HIT_BATCH, filtered);
return await this.performPost(PirschEndpoint.HIT_BATCH, hits);
}

/**
Expand All @@ -151,10 +142,6 @@ export abstract class PirschCoreClient extends PirschCommon {
duration = 0,
meta?: Record<string, Scalar>
): Promise<Optional<PirschApiError>> {
if (hit.dnt === "1") {
return;
}

const event: PirschEvent = {
event_name: name,
event_duration: duration,
Expand All @@ -180,13 +167,7 @@ export abstract class PirschCoreClient extends PirschCommon {
meta?: Record<string, Scalar>;
}[]
): Promise<Optional<PirschApiError>> {
const filtered = events.filter(event => event.hit.dnt !== "1");

if (filtered.length === 0) {
return;
}

const results = filtered.map(({ name, hit, time, duration = 0, meta }) => {
const results = events.map(({ name, hit, time, duration = 0, meta }) => {
const event: PirschBatchEvent = {
event_name: name,
event_duration: duration,
Expand All @@ -208,10 +189,6 @@ export abstract class PirschCoreClient extends PirschCommon {
* @returns APIError or an empty promise, in case something went wrong
*/
async session(session: PirschSession): Promise<Optional<PirschApiError>> {
if (session.dnt === "1") {
return;
}

return await this.performPost(PirschEndpoint.SESSION, session);
}

Expand All @@ -222,13 +199,7 @@ export abstract class PirschCoreClient extends PirschCommon {
* @returns APIError or an empty promise, in case something went wrong
*/
async batchSessions(sessions: PirschBatchSession[]): Promise<Optional<PirschApiError>> {
const filtered = sessions.filter(session => session.dnt !== "1");

if (filtered.length === 0) {
return;
}

return await this.performPost(PirschEndpoint.SESSION_BATCH, filtered);
return await this.performPost(PirschEndpoint.SESSION_BATCH, sessions);
}

/**
Expand Down Expand Up @@ -551,6 +522,17 @@ export abstract class PirschCoreClient extends PirschCommon {
return await this.performFilteredGet<PirschCountryStats[]>(PirschEndpoint.COUNTRY, filter);
}

/**
* region returns regional statistics.
*
* @param filter used to filter the result set.
*/
async region(filter: PirschFilter): Promise<PirschRegionStats[] | PirschApiError> {
this.accessModeCheck("region");

return await this.performFilteredGet<PirschRegionStats[]>(PirschEndpoint.REGION, filter);
}

/**
* city returns city statistics.
*
Expand Down Expand Up @@ -693,6 +675,7 @@ export abstract class PirschCoreClient extends PirschCommon {
event_meta_key: filter.event_meta_key,
language: filter.language,
country: filter.country,
region: filter.region,
city: filter.city,
referrer: filter.referrer,
referrer_name: filter.referrer_name,
Expand Down
15 changes: 12 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export interface PirschAuthenticationResponse {
export interface PirschHit {
url: string;
ip: string;
dnt?: string;
user_agent: string;
accept_language?: string;
sec_ch_ua?: string;
Expand Down Expand Up @@ -145,7 +144,7 @@ export interface PirschBatchEvent extends PirschEvent {
* all other fields can be left empty, but it's highly recommended to send all fields to generate reliable data.
* The fields can be set from the request headers.
*/
export type PirschSession = Pick<PirschEvent, "ip" | "dnt" | "user_agent">;
export type PirschSession = Pick<PirschEvent, "ip" | "user_agent">;

/**
* PirschBatchSession contains all required fields to send batched sessions to Pirsch. The IP and User-Agent are mandatory,
Expand All @@ -164,7 +163,6 @@ export interface PirschBatchSession extends PirschSession {
export interface PirschBrowserHit {
url: string;
title?: string;
dnt?: string;
accept_language?: string;
referrer?: string;
screen_width?: number;
Expand Down Expand Up @@ -220,6 +218,7 @@ export interface PirschFilter {
event_meta?: Record<string, Scalar>;
language?: string;
country?: string;
region?: string;
city?: string;
referrer?: string;
referrer_name?: string;
Expand Down Expand Up @@ -523,10 +522,20 @@ export interface PirschCountryStats extends PirschMetaStats {
country_code: string;
}

/*
* PirschRegionStats is the result type for regional statistics.
*/
export interface PirschRegionStats extends PirschMetaStats {
country_code: string;
region: string;
}

/*
* PirschCityStats is the result type for city statistics.
*/
export interface PirschCityStats extends PirschMetaStats {
country_code: string;
region: string;
city: string;
}

Expand Down
27 changes: 1 addition & 26 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ export class PirschWebClient extends PirschCommon {
public async hit(hit?: Partial<PirschBrowserHit>): Promise<void> {
const data = { ...this.hitFromBrowser(), ...hit };
const parameters = this.browserHitToGetParameters(data);

if (data.dnt === "1") {
return;
}

await this.get(PirschEndpoint.HIT, { parameters });
}

Expand All @@ -93,11 +88,6 @@ export class PirschWebClient extends PirschCommon {
hit?: Partial<PirschBrowserHit>
): Promise<void> {
const data = { ...this.hitFromBrowser(), ...hit };

if (data.dnt === "1") {
return;
}

await this.post(
PirschEndpoint.EVENT,
{
Expand All @@ -118,11 +108,6 @@ export class PirschWebClient extends PirschCommon {
*/
public async customHit(hit: PirschBrowserHit): Promise<void> {
const parameters = this.browserHitToGetParameters(hit);

if (hit.dnt === "1") {
return;
}

await this.get(PirschEndpoint.HIT, { parameters });
}

Expand All @@ -140,10 +125,6 @@ export class PirschWebClient extends PirschCommon {
hit: PirschBrowserHit,
meta?: Record<string, Scalar>
): Promise<void> {
if (hit.dnt === "1") {
return;
}

await this.post(
PirschEndpoint.EVENT,
{
Expand All @@ -165,19 +146,13 @@ export class PirschWebClient extends PirschCommon {
* @returns Hit object containing all necessary fields.
*/
public hitFromBrowser(): PirschBrowserHit {
const element: PirschBrowserHit = {
return {
url: this.generateUrl(),
title: document.title,
referrer: document.referrer,
screen_width: screen.width,
screen_height: screen.height,
};

if (navigator.doNotTrack === "1") {
element.dnt = navigator.doNotTrack;
}

return element;
}

private browserHitToGetParameters(data: PirschBrowserHit) {
Expand Down

0 comments on commit edadfe8

Please sign in to comment.