import "github.com/emvi/pirsch"
analyzer.go filter.go fingerprint.go geodb.go hit.go ip.go log.go mock_store.go model.go postgres.go processor.go referrer_blacklist.go screen.go session.go store.go tracker.go user_agent.go user_agent_blacklist.go user_agent_lists.go user_agent_test_data.go util.go
const ( // BrowserChrome represents the Chrome and Chromium browser. BrowserChrome = "Chrome" // BrowserFirefox represents the Firefox browser. BrowserFirefox = "Firefox" // BrowserSafari represents the Safari browser. BrowserSafari = "Safari" // BrowserOpera represents the Opera browser. BrowserOpera = "Opera" // BrowserEdge represents the Edge browser. BrowserEdge = "Edge" // BrowserIE represents the Internet Explorer browser. BrowserIE = "IE" // OSWindows represents the Windows operating system. OSWindows = "Windows" // OSMac represents the Mac operating system. OSMac = "Mac" // OSLinux represents a Linux distribution. OSLinux = "Linux" // OSAndroid represents the Android operating system. OSAndroid = "Android" // OSiOS represents the iOS operating system. OSiOS = "iOS" // OSWindowsMobile represents the Windows Mobile operating system. OSWindowsMobile = "Windows Mobile" )
const ( // GeoLite2Filename is the default filename of the GeoLite2 database. GeoLite2Filename = "GeoLite2-Country.mmdb" )
var NullTenant = NewTenantID(0)
NullTenant can be used to pass no (null) tenant to filters and functions. This is a sql.NullInt64 with a value of 0.
var ScreenClasses = []screenClass{
{1440, "XXL"},
{1024, "XL"},
{800, "L"},
{600, "M"},
{415, "S"},
}
ScreenClasses is a list of typical screen sizes used to group resolutions. Everything below is considered "XS" (tiny).
Fingerprint returns a hash for given request and salt. The hash is unique for the visitor.
GetGeoLite2 downloads and unpacks the MaxMind GeoLite2 database. The tarball is downloaded and unpacked at the provided path. The directories will created if required. The license key is used for the download and must be provided for a registered account. Please refer to MaxMinds website on how to do that: https://dev.maxmind.com/geoip/geoip2/geolite2/ The database should be updated on a regular basis.
GetScreenClass returns the screen class for given width in pixels.
IgnoreHit returns true, if a hit should be ignored for given request, or false otherwise. The easiest way to track visitors is to use the Tracker.
NewTenantID is a helper function to return a sql.NullInt64. The ID is considered valid if greater than 0.
func RunAtMidnight(f func()) context.CancelFunc
RunAtMidnight calls given function on each day of month on midnight (UTC), unless it is cancelled by calling the cancel function.
type Analyzer struct {
// contains filtered or unexported fields
}
Analyzer provides an interface to analyze processed data and hits.
func NewAnalyzer(store Store, config *AnalyzerConfig) *Analyzer
NewAnalyzer returns a new Analyzer for given Store.
func (analyzer *Analyzer) ActiveVisitors(filter *Filter, duration time.Duration) ([]Stats, int, error)
ActiveVisitors returns the active visitors per path and the total number of active visitors for given duration. Use time.Minute*5 for example to see the active visitors for the past 5 minutes. The correct date/time is not included.
func (analyzer *Analyzer) Browser(filter *Filter) ([]BrowserStats, error)
Browser returns the visitor count per browser.
func (analyzer *Analyzer) Country(filter *Filter) ([]CountryStats, error)
Country returns the visitor count per country.
Growth returns the total number of visitors, sessions, and bounces for given time frame and path and calculates the growth of each metric relative to the previous time frame. The path is optional. It does not include today, as that won't be accurate (the day needs to be over to be comparable).
func (analyzer *Analyzer) Languages(filter *Filter) ([]LanguageStats, error)
Languages returns the visitor count per language.
OS returns the visitor count per operating system.
func (analyzer *Analyzer) PageBrowser(filter *Filter) ([]BrowserStats, error)
PageBrowser returns the visitor count per brower, day, path, and for the given time frame. The path is mandatory.
func (analyzer *Analyzer) PageLanguages(filter *Filter) ([]LanguageStats, error)
PageLanguages returns the visitor count per language, day, path, and for the given time frame. The path is mandatory.
PageOS returns the visitor count per operating system, day, path, and for the given time frame. The path is mandatory.
func (analyzer *Analyzer) PagePlatform(filter *Filter) *VisitorStats
PagePlatform returns the visitor count per platform, day, path, and for the given time frame. The path is mandatory.
func (analyzer *Analyzer) PageReferrer(filter *Filter) ([]ReferrerStats, error)
PageReferrer returns the visitor count per referrer, day, path, and for the given time frame. The path is mandatory.
func (analyzer *Analyzer) PageVisitors(filter *Filter) ([]PathVisitors, error)
PageVisitors returns the visitor count, session count, and bounce rate per day for the given time frame grouped by path.
func (analyzer *Analyzer) Platform(filter *Filter) *VisitorStats
Platform returns the visitor count per browser.
func (analyzer *Analyzer) Referrer(filter *Filter) ([]ReferrerStats, error)
Referrer returns the visitor count per referrer.
func (analyzer *Analyzer) Screen(filter *Filter) ([]ScreenStats, error)
Screen returns the visitor count per screen size (width and height).
func (analyzer *Analyzer) ScreenClass(filter *Filter) ([]ScreenStats, error)
ScreenClass returns the visitor count per screen class.
func (analyzer *Analyzer) TimeOfDay(filter *Filter) ([]TimeOfDayVisitors, error)
TimeOfDay returns the visitor count per day and hour for given time frame.
func (analyzer *Analyzer) VisitorHours(filter *Filter) ([]VisitorTimeStats, error)
VisitorHours returns the visitor count grouped by hour of day for given time frame. Note that the sum of them is not the number of unique visitors for the day, as visitors can re-appear at different times on the same day!
Visitors returns the visitor count, session count, and bounce rate per day.
type AnalyzerConfig struct { // Timezone sets the time zone for the result set. // If not set, UTC will be used. Timezone *time.Location }
AnalyzerConfig is the (optional) configuration for the Analyzer.
type BaseEntity struct { ID int64 `db:"id" json:"id"` TenantID sql.NullInt64 `db:"tenant_id" json:"tenant_id"` }
BaseEntity is the base entity for all other entities.
type BrowserStats struct { Stats Browser sql.NullString `db:"browser" json:"browser"` BrowserVersion sql.NullString `db:"browser_version" json:"version"` }
BrowserStats is the visitor count for each path on each day and browser.
type CountryStats struct { Stats CountryCode sql.NullString `db:"country_code" json:"country_code"` }
CountryStats is the visitor count for each country on each day.
type Filter struct { // TenantID is the optional tenant ID used to filter results. TenantID sql.NullInt64 // Path is the optional path for the selection. Path string // From is the start of the selection. From time.Time // To is the end of the selection. To time.Time }
Filter is used to specify the time frame, path and tenant for the Analyzer.
NewFilter returns a new default filter for given tenant and the past week.
Days returns the number of days covered by the filter.
type GeoDB struct {
// contains filtered or unexported fields
}
GeoDB maps IPs to their geo location based on MaxMinds GeoLite2 or GeoIP2 database.
func NewGeoDB(config GeoDBConfig) (*GeoDB, error)
NewGeoDB creates a new GeoDB for given database file. Make sure you call GeoDB.Close to release the system resources! The database should be updated on a regular basis.
Close closes the database file handle and frees the system resources. It's important to call this when you don't need the GeoDB anymore!
CountryCode looks up the country code for given IP. If the IP is invalid it will return an empty string. The country code is returned in lowercase.
type GeoDBConfig struct { // File is the path (including the filename) to the GeoLite2 country database file. // See GeoLite2Filename for the required filename. File string // Logger is the log.Logger used for logging. // Note that this will log the IP address and should therefore only be used for debugging. // Set it to nil to disable logging for GeoDB. Logger *log.Logger }
GeoDBConfig is the configuration for the GeoDB.
type Growth struct { Current *Stats `json:"current"` Previous *Stats `json:"previous"` VisitorsGrowth float64 `json:"visitors_growth"` SessionsGrowth float64 `json:"sessions_growth"` BouncesGrowth float64 `json:"bounces_growth"` }
Growth represents the visitors, sessions, and bounces growth between two time periods.
type Hit struct { BaseEntity Fingerprint string `db:"fingerprint" json:"fingerprint"` Session sql.NullTime `db:"session" json:"session"` Path string `db:"path" json:"path"` URL sql.NullString `db:"url" json:"url,omitempty"` Language sql.NullString `db:"language" json:"language,omitempty"` UserAgent sql.NullString `db:"user_agent" json:"user_agent,omitempty"` Referrer sql.NullString `db:"referrer" json:"referrer,omitempty"` OS sql.NullString `db:"os" json:"os,omitempty"` OSVersion sql.NullString `db:"os_version" json:"os_version,omitempty"` Browser sql.NullString `db:"browser" json:"browser,omitempty"` BrowserVersion sql.NullString `db:"browser_version" json:"browser_version,omitempty"` CountryCode sql.NullString `db:"country_code" json:"country_code"` Desktop bool `db:"desktop" json:"desktop"` Mobile bool `db:"mobile" json:"mobile"` ScreenWidth int `db:"screen_width" json:"screen_width"` ScreenHeight int `db:"screen_height" json:"screen_height"` ScreenClass sql.NullString `db:"screen_class" json:"screen_class"` Time time.Time `db:"time" json:"time"` }
Hit represents a single data point/page visit and is the central entity of Pirsch.
HitFromRequest returns a new Hit for given request, salt and HitOptions. The salt must stay consistent to track visitors across multiple calls. The easiest way to track visitors is to use the Tracker.
String implements the Stringer interface.
type HitOptions struct { // TenantID is optionally saved with a hit to split the data between multiple tenants. TenantID sql.NullInt64 // URL can be set to manually overwrite the URL stored for this request. // This will also affect the Path, except it is set too. URL string // Path can be set to manually overwrite the path stored for the request. // This will also affect the URL. Path string // Referrer can be set to manually overwrite the referrer from the request. Referrer string // ReferrerDomainBlacklist is used to filter out unwanted referrers from the Referrer header. // This can be used to filter out traffic from your own site or subdomains. // To filter your own domain and subdomains, add your domain to the list and set ReferrerDomainBlacklistIncludesSubdomains to true. // This way the referrer for blog.mypage.com -> mypage.com won't be saved. ReferrerDomainBlacklist []string // ReferrerDomainBlacklistIncludesSubdomains set to true to include all subdomains in the ReferrerDomainBlacklist, // or else subdomains must explicitly be included in the blacklist. // If the blacklist contains domain.com, sub.domain.com and domain.com will be treated as equals. ReferrerDomainBlacklistIncludesSubdomains bool // ScreenWidth sets the screen width to be stored with the hit. ScreenWidth int // ScreenHeight sets the screen height to be stored with the hit. ScreenHeight int // contains filtered or unexported fields }
HitOptions is used to manipulate the data saved on a hit.
func HitOptionsFromRequest(r *http.Request) *HitOptions
HitOptionsFromRequest returns the HitOptions for given client request. This function can be used to accept hits from pirsch.js. Invalid parameters are ignored and left empty. You might want to add additional checks before calling HitFromRequest afterwards (like for the HitOptions.TenantID).
type LanguageStats struct { Stats Language sql.NullString `db:"language" json:"language"` }
LanguageStats is the visitor count for each path on each day and language.
type OSStats struct { Stats OS sql.NullString `db:"os" json:"os"` OSVersion sql.NullString `db:"os_version" json:"version"` }
OSStats is the visitor count for each path on each day and operating system.
PathVisitors represents visitor statistics per day for a path.
type PostgresConfig struct { // Logger is the log.Logger used for logging. // The default log will be used printing to os.Stdout with "pirsch" in its prefix in case it is not set. Logger *log.Logger }
PostgresConfig is the optional configuration for the PostgresStore.
PostgresStore implements the Store interface.
func NewPostgresStore(db *sql.DB, config *PostgresConfig) *PostgresStore
NewPostgresStore creates a new postgres storage for given database connection and logger.
func (store *PostgresStore) ActivePageVisitors(tenantID sql.NullInt64, from time.Time) ([]Stats, error)
ActivePageVisitors implements the Store interface.
ActiveVisitors implements the Store interface.
func (store *PostgresStore) Commit(tx *sqlx.Tx)
Commit implements the Store interface.
func (store *PostgresStore) CountVisitors(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) *Stats
CountVisitors implements the Store interface.
func (store *PostgresStore) CountVisitorsByBrowser(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]BrowserStats, error)
CountVisitorsByBrowser implements the Store interface.
func (store *PostgresStore) CountVisitorsByCountryCode(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]CountryStats, error)
CountVisitorsByCountryCode implements the Store interface.
func (store *PostgresStore) CountVisitorsByHour(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]VisitorTimeStats, error)
CountVisitorsByPathAndHour implements the Store interface.
func (store *PostgresStore) CountVisitorsByLanguage(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]LanguageStats, error)
CountVisitorsByLanguage implements the Store interface.
func (store *PostgresStore) CountVisitorsByOS(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]OSStats, error)
CountVisitorsByOS implements the Store interface.
func (store *PostgresStore) CountVisitorsByPath(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string, includePlatform bool) ([]VisitorStats, error)
CountVisitorsByPath implements the Store interface.
func (store *PostgresStore) CountVisitorsByPathAndBrowser(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]BrowserStats, error)
CountVisitorsByPathAndBrowser implements the Store interface.
func (store *PostgresStore) CountVisitorsByPathAndLanguage(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]LanguageStats, error)
CountVisitorsByPathAndLanguage implements the Store interface.
func (store *PostgresStore) CountVisitorsByPathAndMaxOneHit(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) int
CountVisitorsByPathAndMaxOneHit implements the Store interface.
func (store *PostgresStore) CountVisitorsByPathAndOS(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]OSStats, error)
CountVisitorsByPathAndOS implements the Store interface.
func (store *PostgresStore) CountVisitorsByPathAndReferrer(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]ReferrerStats, error)
CountVisitorsByPathAndReferrer implements the Store interface.
func (store *PostgresStore) CountVisitorsByPlatform(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) *VisitorStats
CountVisitorsByPlatform implements the Store interface.
func (store *PostgresStore) CountVisitorsByReferrer(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]ReferrerStats, error)
CountVisitorsByReferrer implements the Store interface.
func (store *PostgresStore) CountVisitorsByScreenClass(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]ScreenStats, error)
CountVisitorsByScreenClass implements the Store interface.
func (store *PostgresStore) CountVisitorsByScreenSize(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]ScreenStats, error)
CountVisitorsByScreenSize implements the Store interface.
func (store *PostgresStore) DeleteHitsByDay(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) error
DeleteHitsByDay implements the Store interface.
HitDays implements the Store interface.
HitPaths implements the Store interface.
func (store *PostgresStore) NewTx() *sqlx.Tx
NewTx implements the Store interface.
func (store *PostgresStore) PageBrowser(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]BrowserStats, error)
PageBrowser implements the Store interface.
func (store *PostgresStore) PageLanguages(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]LanguageStats, error)
PageLanguages implements the Store interface.
func (store *PostgresStore) PageOS(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]OSStats, error)
PageOS implements the Store interface.
func (store *PostgresStore) PagePlatform(tenantID sql.NullInt64, path string, from time.Time, to time.Time) *VisitorStats
PagePlatform implements the Store interface.
func (store *PostgresStore) PageReferrer(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]ReferrerStats, error)
PageReferrer implements the Store interface.
func (store *PostgresStore) PageVisitors(tenantID sql.NullInt64, path string, from, to time.Time) ([]Stats, error)
PageVisitors implements the Store interface.
Paths implements the Store interface.
func (store *PostgresStore) Rollback(tx *sqlx.Tx)
Rollback implements the Store interface.
func (store *PostgresStore) SaveBrowserStats(tx *sqlx.Tx, entity *BrowserStats) error
SaveBrowserStats implements the Store interface.
func (store *PostgresStore) SaveCountryStats(tx *sqlx.Tx, entity *CountryStats) error
SaveCountryStats implements the Store interface.
func (store *PostgresStore) SaveHits(hits []Hit) error
SaveHits implements the Store interface.
func (store *PostgresStore) SaveLanguageStats(tx *sqlx.Tx, entity *LanguageStats) error
SaveLanguageStats implements the Store interface.
SaveOSStats implements the Store interface.
func (store *PostgresStore) SaveReferrerStats(tx *sqlx.Tx, entity *ReferrerStats) error
SaveReferrerStats implements the Store interface.
func (store *PostgresStore) SaveScreenStats(tx *sqlx.Tx, entity *ScreenStats) error
SaveScreenStats implements the Store interface.
func (store *PostgresStore) SaveVisitorStats(tx *sqlx.Tx, entity *VisitorStats) error
SaveVisitorStats implements the Store interface.
func (store *PostgresStore) SaveVisitorTimeStats(tx *sqlx.Tx, entity *VisitorTimeStats) error
SaveVisitorTimeStats implements the Store interface.
func (store *PostgresStore) Session(tenantID sql.NullInt64, fingerprint string, maxAge time.Time) time.Time
Session implements the Store interface.
func (store *PostgresStore) VisitorBrowser(tenantID sql.NullInt64, from, to time.Time) ([]BrowserStats, error)
VisitorBrowser implements the Store interface.
func (store *PostgresStore) VisitorCountry(tenantID sql.NullInt64, from, to time.Time) ([]CountryStats, error)
VisitorCountry implements the Store interface.
func (store *PostgresStore) VisitorHours(tenantID sql.NullInt64, from time.Time, to time.Time) ([]VisitorTimeStats, error)
VisitorHours implements the Store interface.
func (store *PostgresStore) VisitorLanguages(tenantID sql.NullInt64, from, to time.Time) ([]LanguageStats, error)
VisitorLanguages implements the Store interface.
func (store *PostgresStore) VisitorOS(tenantID sql.NullInt64, from, to time.Time) ([]OSStats, error)
VisitorOS implements the Store interface.
func (store *PostgresStore) VisitorPlatform(tenantID sql.NullInt64, from, to time.Time) *VisitorStats
VisitorPlatform implements the Store interface.
func (store *PostgresStore) VisitorReferrer(tenantID sql.NullInt64, from, to time.Time) ([]ReferrerStats, error)
VisitorReferrer implements the Store interface.
func (store *PostgresStore) VisitorScreenClass(tenantID sql.NullInt64, from, to time.Time) ([]ScreenStats, error)
VisitorScreenClass implements the Store interface.
func (store *PostgresStore) VisitorScreenSize(tenantID sql.NullInt64, from, to time.Time) ([]ScreenStats, error)
VisitorScreenSize implements the Store interface.
Visitors implements the Store interface.
func (store *PostgresStore) VisitorsSum(tenantID sql.NullInt64, from, to time.Time, path string) (*Stats, error)
VisitorsSum implements the Store interface.
type Processor struct {
// contains filtered or unexported fields
}
Processor processes hits to reduce them into meaningful statistics.
NewProcessor creates a new Processor for given Store.
Process processes all hits in database and deletes them afterwards.
ProcessTenant processes all hits in database for given tenant and deletes them afterwards. The tenant can be set to nil if you don't split your data (which is usually the case).
type ReferrerStats struct { Stats Referrer sql.NullString `db:"referrer" json:"referrer"` }
ReferrerStats is the visitor count for each path on each day and referrer.
type ScreenStats struct { Stats Width int `db:"width" json:"width"` Height int `db:"height" json:"height"` Class sql.NullString `db:"class" json:"class"` }
ScreenStats is the visitor count for each screen resolution on each day.
type Stats struct { BaseEntity Day time.Time `db:"day" json:"day"` Path sql.NullString `db:"path" json:"path"` Visitors int `db:"visitors" json:"visitors"` Sessions int `db:"sessions" json:"sessions"` Bounces int `db:"bounces" json:"bounces"` RelativeVisitors float64 `db:"-" json:"relative_visitors"` BounceRate float64 `db:"-" json:"bounce_rate"` }
Stats is the base entity for all statistics.
GetID returns the ID.
GetVisitors returns the visitor count.
type Store interface { // NewTx creates a new transaction and panic on failure. NewTx() *sqlx.Tx // Commit commits given transaction and logs the error. Commit(*sqlx.Tx) // Rollback rolls back given transaction and logs the error. Rollback(*sqlx.Tx) // SaveHits persists a list of hits. SaveHits([]Hit) error // DeleteHitsByDay deletes all hits on given day. DeleteHitsByDay(*sqlx.Tx, sql.NullInt64, time.Time) error // SaveVisitorStats saves VisitorStats. SaveVisitorStats(*sqlx.Tx, *VisitorStats) error // SaveVisitorTimeStats saves VisitorTimeStats. SaveVisitorTimeStats(*sqlx.Tx, *VisitorTimeStats) error // SaveLanguageStats saves LanguageStats. SaveLanguageStats(*sqlx.Tx, *LanguageStats) error // SaveReferrerStats saves ReferrerStats. SaveReferrerStats(*sqlx.Tx, *ReferrerStats) error // SaveOSStats saves OSStats. SaveOSStats(*sqlx.Tx, *OSStats) error // SaveBrowserStats saves BrowserStats. SaveBrowserStats(*sqlx.Tx, *BrowserStats) error // SaveScreenStats saves ScreenStats. SaveScreenStats(*sqlx.Tx, *ScreenStats) error // SaveCountryStats saves CountryStats. SaveCountryStats(*sqlx.Tx, *CountryStats) error // Session returns the hits session timestamp for given fingerprint and max age. Session(sql.NullInt64, string, time.Time) time.Time // HitDays returns the distinct days with at least one hit. HitDays(sql.NullInt64) ([]time.Time, error) // HitPaths returns the distinct paths for given day. HitPaths(sql.NullInt64, time.Time) ([]string, error) // Paths returns the distinct paths for given time frame. Paths(sql.NullInt64, time.Time, time.Time) ([]string, error) // CountVisitors returns the visitor count for given day. CountVisitors(*sqlx.Tx, sql.NullInt64, time.Time) *Stats // CountVisitorsByPath returns the visitor count for given day, path, and if the platform should be included or not. CountVisitorsByPath(*sqlx.Tx, sql.NullInt64, time.Time, string, bool) ([]VisitorStats, error) // CountVisitorsByPathAndHour returns the visitor count for given day and path grouped by hour of day. CountVisitorsByHour(*sqlx.Tx, sql.NullInt64, time.Time) ([]VisitorTimeStats, error) // CountVisitorsByPathAndLanguage returns the visitor count for given day and path grouped by language. CountVisitorsByPathAndLanguage(*sqlx.Tx, sql.NullInt64, time.Time, string) ([]LanguageStats, error) // CountVisitorsByPathAndReferrer returns the visitor count for given day and path grouped by referrer. CountVisitorsByPathAndReferrer(*sqlx.Tx, sql.NullInt64, time.Time, string) ([]ReferrerStats, error) // CountVisitorsByPathAndOS returns the visitor count for given day and path grouped by operating system and operating system version. CountVisitorsByPathAndOS(*sqlx.Tx, sql.NullInt64, time.Time, string) ([]OSStats, error) // CountVisitorsByPathAndBrowser returns the visitor count for given day and path grouped by browser and browser version. CountVisitorsByPathAndBrowser(*sqlx.Tx, sql.NullInt64, time.Time, string) ([]BrowserStats, error) // CountVisitorsByLanguage returns the visitor count for given day grouped by language. CountVisitorsByLanguage(*sqlx.Tx, sql.NullInt64, time.Time) ([]LanguageStats, error) // CountVisitorsByReferrer returns the visitor count for given day grouped by referrer. CountVisitorsByReferrer(*sqlx.Tx, sql.NullInt64, time.Time) ([]ReferrerStats, error) // CountVisitorsByOS returns the visitor count for given day grouped by operating system. CountVisitorsByOS(*sqlx.Tx, sql.NullInt64, time.Time) ([]OSStats, error) // CountVisitorsByBrowser returns the visitor count for given day grouped by browser. CountVisitorsByBrowser(*sqlx.Tx, sql.NullInt64, time.Time) ([]BrowserStats, error) // CountVisitorsByScreenSize returns the visitor count for given day grouped by screen size (width and height). CountVisitorsByScreenSize(*sqlx.Tx, sql.NullInt64, time.Time) ([]ScreenStats, error) // CountVisitorsByScreenClass returns the visitor count for given day grouped by screen class. CountVisitorsByScreenClass(*sqlx.Tx, sql.NullInt64, time.Time) ([]ScreenStats, error) // CountVisitorsByCountryCode returns the visitor count for given day grouped by country code. CountVisitorsByCountryCode(*sqlx.Tx, sql.NullInt64, time.Time) ([]CountryStats, error) // CountVisitorsByPlatform returns the visitor count for given day grouped by platform. CountVisitorsByPlatform(*sqlx.Tx, sql.NullInt64, time.Time) *VisitorStats // CountVisitorsByPathAndMaxOneHit returns the visitor count for given day and optional path with a maximum of one hit. // This returns the absolut number of hits without further page calls and is used to calculate the bounce rate. CountVisitorsByPathAndMaxOneHit(*sqlx.Tx, sql.NullInt64, time.Time, string) int // ActiveVisitors returns the active visitor count for given duration. ActiveVisitors(sql.NullInt64, time.Time) int // ActivePageVisitors returns the active visitors grouped by path for given duration. ActivePageVisitors(sql.NullInt64, time.Time) ([]Stats, error) // Visitors returns the visitors for given time frame grouped by days. Visitors(sql.NullInt64, time.Time, time.Time) ([]Stats, error) // VisitorHours returns the visitors for given time frame grouped by hour of day. VisitorHours(sql.NullInt64, time.Time, time.Time) ([]VisitorTimeStats, error) // VisitorLanguages returns the visitors for given time frame grouped by language. VisitorLanguages(sql.NullInt64, time.Time, time.Time) ([]LanguageStats, error) // VisitorReferrer returns the visitor count for given time frame grouped by referrer. VisitorReferrer(sql.NullInt64, time.Time, time.Time) ([]ReferrerStats, error) // VisitorOS returns the visitor count for given time frame grouped by operating system. VisitorOS(sql.NullInt64, time.Time, time.Time) ([]OSStats, error) // VisitorBrowser returns the visitor count for given time frame grouped by browser. VisitorBrowser(sql.NullInt64, time.Time, time.Time) ([]BrowserStats, error) // VisitorPlatform returns the visitor count for given time frame grouped by platform. VisitorPlatform(sql.NullInt64, time.Time, time.Time) *VisitorStats // VisitorScreenSize returns the visitor count for given time frame grouped by screen size (width and height). VisitorScreenSize(sql.NullInt64, time.Time, time.Time) ([]ScreenStats, error) // VisitorScreenClass returns the visitor count for given time frame grouped by screen class. VisitorScreenClass(sql.NullInt64, time.Time, time.Time) ([]ScreenStats, error) // VisitorCountry returns the visitor count for given time frame grouped by country code. VisitorCountry(sql.NullInt64, time.Time, time.Time) ([]CountryStats, error) // PageVisitors returns the visitors for given path and time frame grouped by days. PageVisitors(sql.NullInt64, string, time.Time, time.Time) ([]Stats, error) // PageReferrer returns the visitors for given path and time frame grouped by referrer. PageReferrer(sql.NullInt64, string, time.Time, time.Time) ([]ReferrerStats, error) // PageLanguages returns the visitors for given path and time frame grouped by language. PageLanguages(sql.NullInt64, string, time.Time, time.Time) ([]LanguageStats, error) // PageOS returns the visitors for given path and time frame grouped by operating system. PageOS(sql.NullInt64, string, time.Time, time.Time) ([]OSStats, error) // PageBrowser returns the visitors for given path and time frame grouped by browser. PageBrowser(sql.NullInt64, string, time.Time, time.Time) ([]BrowserStats, error) // PagePlatform returns the visitors for given path and time frame grouped by platform. PagePlatform(sql.NullInt64, string, time.Time, time.Time) *VisitorStats // VisitorsSum returns the sum of the visitors, sessions, and bounces for given time frame and path. // The path is optional. VisitorsSum(sql.NullInt64, time.Time, time.Time, string) (*Stats, error) }
Store defines an interface to persists hits and other data. The first parameter (if required) is always the tenant ID and can be left out (pirsch.NullTenant), if you don't want to split your data. This is usually the case if you integrate Pirsch into your application.
type TimeOfDayVisitors struct { Day time.Time `json:"day"` Stats []VisitorTimeStats `json:"stats"` }
TimeOfDayVisitors represents the visitor count per day and hour for a path.
type Tracker struct {
// contains filtered or unexported fields
}
Tracker is the main component of Pirsch. It provides methods to track requests and store them in a data store. Make sure you call Stop to make sure the hits get stored before shutting down the server.
func NewTracker(store Store, salt string, config *TrackerConfig) *Tracker
NewTracker creates a new tracker for given store, salt and config. Pass nil for the config to use the defaults. The salt is mandatory.
Flush flushes all hits to store that are currently buffered by the workers. Call Tracker.Stop to also save hits that are in the queue.
func (tracker *Tracker) Hit(r *http.Request, options *HitOptions)
Hit stores the given request. The request might be ignored if it meets certain conditions. The HitOptions, if passed, will overwrite the Tracker configuration. It's save (and recommended!) to call this function in its own goroutine.
SetGeoDB sets the GeoDB for the Tracker. The call to this function is thread safe to enable live updates of the database. Pass nil to disable the feature.
Stop flushes and stops all workers.
type TrackerConfig struct { // Worker sets the number of workers that are used to store hits. // Must be greater or equal to 1. Worker int // WorkerBufferSize is the size of the buffer used to store hits. // Must be greater than 0. The hits are stored in batch when the buffer is full. WorkerBufferSize int // WorkerTimeout sets the timeout used to store hits. // This is used to allow the workers to store hits even if the buffer is not full yet. // It's recommended to set this to a few seconds. // If you leave it 0, the default timeout is used, else it is limted to 60 seconds. WorkerTimeout time.Duration // ReferrerDomainBlacklist see HitOptions.ReferrerDomainBlacklist. ReferrerDomainBlacklist []string // ReferrerDomainBlacklistIncludesSubdomains see HitOptions.ReferrerDomainBlacklistIncludesSubdomains. ReferrerDomainBlacklistIncludesSubdomains bool // Sessions enables/disables session tracking. // It's enabled by default. Sessions bool // SessionMaxAge is used to define how long a session runs at maximum. // Set to two hours by default. SessionMaxAge time.Duration // SessionCleanupInterval sets the session cache lifetime. // If not passed, the default will be used. SessionCleanupInterval time.Duration // GeoDB enables/disabled mapping IPs to country codes. // Can be set/updated at runtime by calling Tracker.SetGeoDB. GeoDB *GeoDB // Logger is the log.Logger used for logging. // The default log will be used printing to os.Stdout with "pirsch" in its prefix in case it is not set. Logger *log.Logger }
TrackerConfig is the optional configuration for the Tracker.
type UserAgent struct { // Browser is the browser name. Browser string // BrowserVersion is the browser (non technical) version number. BrowserVersion string // OS is the operating system. OS string // OSVersion is the operating system version number. OSVersion string }
UserAgent contains information extracted from the User-Agent header.
ParseUserAgent parses given User-Agent header and returns the extracted information. This just supports major browsers and operating systems, we don't care about browsers and OSes that have no market share, unless you prove us wrong.
IsDesktop returns true if the user agent is a desktop device.
IsMobile returns true if the user agent is a mobile device.
type VisitorStats struct { Stats PlatformDesktop int `db:"platform_desktop" json:"platform_desktop"` PlatformMobile int `db:"platform_mobile" json:"platform_mobile"` PlatformUnknown int `db:"platform_unknown" json:"platform_unknown"` RelativePlatformDesktop float64 `db:"-" json:"relative_platform_desktop"` RelativePlatformMobile float64 `db:"-" json:"relative_platform_mobile"` RelativePlatformUnknown float64 `db:"-" json:"relative_platform_unknown"` }
VisitorStats is the visitor count for each path on each day and platform and it is used to calculate the total visitor count for each day.
VisitorTimeStats is the visitor count for each path on each day and hour (ranging from 0 to 23).
Package pirsch imports 27 packages (graph). Updated 2021-01-22. Refresh now. Tools for package owners.