Skip to content

Commit

Permalink
Added missing endpoints and filter options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Oct 25, 2021
1 parent 2b6257d commit 452be73
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.3.0

* added endpoint to extend sessions
* added entry page statistics
* added exit page statistics
* added number of sessions to referrer statistics
* added city statistics
* added entry page, exit page, city, and referrer name to filter

## 1.2.0

* added method to send events
Expand Down
75 changes: 73 additions & 2 deletions Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ import {
UTMSourceStats,
TimeSpentStats
} from "./types";
import { EntryStats } from ".";
import { ExitStats } from ".";
import { CityStats } from ".";

const defaultBaseURL = "https://api.pirsch.io";
const defaultTimeout = 5000;
const defaultProtocol = "http";
const authenticationEndpoint = "/api/v1/token";
const hitEndpoint = "/api/v1/hit";
const eventEndpoint = "/api/v1/event";
const sessionEndpoint = "/api/v1/session";
const domainEndpoint = "/api/v1/domain";
const sessionDurationEndpoint = "/api/v1/statistics/duration/session";
const timeOnPageEndpoint = "/api/v1/statistics/duration/page";
Expand All @@ -47,6 +51,8 @@ const utmContentEndpoint = "/api/v1/statistics/utm/content";
const utmTermEndpoint = "/api/v1/statistics/utm/term";
const visitorsEndpoint = "/api/v1/statistics/visitor";
const pagesEndpoint = "/api/v1/statistics/page";
const entryPagesEndpoint = "/api/v1/statistics/page/entry";
const exitPagesEndpoint = "/api/v1/statistics/page/exit";
const conversionGoalsEndpoint = "/api/v1/statistics/goals";
const eventsEndpoint = "/api/v1/statistics/events";
const eventMetadataEndpoint = "/api/v1/statistics/event/meta";
Expand All @@ -58,6 +64,7 @@ const referrerEndpoint = "/api/v1/statistics/referrer";
const osEndpoint = "/api/v1/statistics/os";
const browserEndpoint = "/api/v1/statistics/browser";
const countryEndpoint = "/api/v1/statistics/country";
const cityEndpoint = "/api/v1/statistics/city";
const platformEndpoint = "/api/v1/statistics/platform";
const screenEndpoint = "/api/v1/statistics/screen";
const keywordsEndpoint = "/api/v1/statistics/keywords";
Expand All @@ -70,7 +77,7 @@ const referrerQueryParams = [
];

/**
* Client is the Pirsch API client.
* Client is used to access the Pirsch API.
*/
export class Client {
private readonly clientID: string;
Expand Down Expand Up @@ -192,6 +199,43 @@ export class Client {
}
}

/**
* session keeps a session alive.
*
* @param hit all required data for the request.
* @param retry retry the request in case a 401 (unauthenticated) error is returned. Don't modify this.
* @returns APIError or an empty promise, in case something went wrong
*/
async session(hit: Hit, retry: boolean = true): Promise<APIError | null> {
try {
if(hit.dnt === "1") {
return Promise.resolve<null>(null);
}

await this.client.post(sessionEndpoint, {
hostname: this.hostname,
...hit,
}, {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.accessToken}`
}
});
return Promise.resolve<null>(null);
} catch(e) {
if(e.response.status === 401 && retry) {
try {
await this.refreshToken();
return this.session(hit, false);
} catch(e) {
return e;
}
}

return Promise.reject<APIError>(e.response.data);
}
}

/**
* hitFromRequest returns the required data to send a hit to Pirsch for a Node request object.
*
Expand Down Expand Up @@ -322,12 +366,30 @@ export class Client {
return await this.performGet<VisitorStats[]>(visitorsEndpoint, filter);
}

/**
* entryPages returns the entry page statistics grouped by page.
*
* @param filter used to filter the result set.
*/
async entryPages(filter: Filter): Promise<EntryStats[] | APIError> {
return await this.performGet<EntryStats[]>(entryPagesEndpoint, filter);
}

/**
* exitPages returns the exit page statistics grouped by page.
*
* @param filter used to filter the result set.
*/
async exitPages(filter: Filter): Promise<ExitStats[] | APIError> {
return await this.performGet<ExitStats[]>(exitPagesEndpoint, filter);
}

/**
* pages returns the page statistics grouped by page.
*
* @param filter used to filter the result set.
*/
async pages(filter: Filter): Promise<PageStats[] | APIError> {
async pages(filter: Filter): Promise<PageStats[] | APIError> {
return await this.performGet<PageStats[]>(pagesEndpoint, filter);
}

Expand Down Expand Up @@ -430,6 +492,15 @@ export class Client {
return await this.performGet<CountryStats[]>(countryEndpoint, filter);
}

/**
* city returns city statistics.
*
* @param filter used to filter the result set.
*/
async city(filter: Filter): Promise<CityStats[] | APIError> {
return await this.performGet<CityStats[]>(cityEndpoint, filter);
}

/**
* platform returns the platforms used by visitors.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pirsch-sdk",
"version": "1.2.0",
"version": "1.3.0",
"description": "JavaScript client SDK for Pirsch.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
37 changes: 37 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ export interface Filter {
to: Date
path?: string
pattern?: string
entry_path?: string
exit_path?: string
event?: string
event_meta_key?: string
language?: string
country?: string
city?: string
referrer?: string
referrer_name?: string
os?: string
browser?: string
platform?: string
Expand Down Expand Up @@ -182,6 +186,31 @@ export interface PageStats {
average_time_spent_seconds: number
}

/*
* EntryStats is the result type for entry page statistics.
*/
export interface EntryStats {
path: string
title: string
visitors: number
sessions: number
entries: number
entry_rate: number
average_time_spent_seconds: number
}

/*
* ExitStats is the result type for exit page statistics.
*/
export interface ExitStats {
exit_path: string
title: string
visitors: number
sessions: number
exits: number
exit_rate: number
}

/**
* ConversionGoal is a conversion goal as configured on the dashboard.
*/
Expand Down Expand Up @@ -273,6 +302,13 @@ export interface CountryStats extends MetaStats {
country_code: string
}

/*
* CityStats is the result type for city statistics.
*/
export interface CityStats extends MetaStats {
city: string
}

/**
* BrowserStats is the result export interface for browser statistics.
*/
Expand All @@ -295,6 +331,7 @@ export interface ReferrerStats {
referrer_name: string
referrer_icon: string
visitors: number
sessions: number
relative_visitors: number
bounces: number
bounce_rate: number
Expand Down

0 comments on commit 452be73

Please sign in to comment.