diff --git a/CHANGELOG.md b/CHANGELOG.md index ac1fedb..ded960e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Client.ts b/Client.ts index fc8e7fe..2f2b6e0 100644 --- a/Client.ts +++ b/Client.ts @@ -30,6 +30,9 @@ import { UTMSourceStats, TimeSpentStats } from "./types"; +import { EntryStats } from "."; +import { ExitStats } from "."; +import { CityStats } from "."; const defaultBaseURL = "https://api.pirsch.io"; const defaultTimeout = 5000; @@ -37,6 +40,7 @@ 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"; @@ -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"; @@ -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"; @@ -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; @@ -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 { + try { + if(hit.dnt === "1") { + return Promise.resolve(null); + } + + await this.client.post(sessionEndpoint, { + hostname: this.hostname, + ...hit, + }, { + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${this.accessToken}` + } + }); + return Promise.resolve(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(e.response.data); + } + } + /** * hitFromRequest returns the required data to send a hit to Pirsch for a Node request object. * @@ -322,12 +366,30 @@ export class Client { return await this.performGet(visitorsEndpoint, filter); } + /** + * entryPages returns the entry page statistics grouped by page. + * + * @param filter used to filter the result set. + */ + async entryPages(filter: Filter): Promise { + return await this.performGet(entryPagesEndpoint, filter); + } + + /** + * exitPages returns the exit page statistics grouped by page. + * + * @param filter used to filter the result set. + */ + async exitPages(filter: Filter): Promise { + return await this.performGet(exitPagesEndpoint, filter); + } + /** * pages returns the page statistics grouped by page. * * @param filter used to filter the result set. */ - async pages(filter: Filter): Promise { + async pages(filter: Filter): Promise { return await this.performGet(pagesEndpoint, filter); } @@ -430,6 +492,15 @@ export class Client { return await this.performGet(countryEndpoint, filter); } + /** + * city returns city statistics. + * + * @param filter used to filter the result set. + */ + async city(filter: Filter): Promise { + return await this.performGet(cityEndpoint, filter); + } + /** * platform returns the platforms used by visitors. * diff --git a/package.json b/package.json index d272f5a..347ce2d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/types.ts b/types.ts index d5d93d1..805917c 100644 --- a/types.ts +++ b/types.ts @@ -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 @@ -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. */ @@ -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. */ @@ -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