pirsch: github.com/emvi/pirsch Index | Files

package pirsch

import "github.com/emvi/pirsch"

Index

Package Files

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

Constants

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"
)

Variables

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).

func Fingerprint Uses

func Fingerprint(r *http.Request, salt string) string

Fingerprint returns a hash for given request and salt. The hash is unique for the visitor.

func GetGeoLite2 Uses

func GetGeoLite2(path, licenseKey string) error

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.

func GetScreenClass Uses

func GetScreenClass(width int) string

GetScreenClass returns the screen class for given width in pixels.

func IgnoreHit Uses

func IgnoreHit(r *http.Request) bool

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.

func NewTenantID Uses

func NewTenantID(id int64) sql.NullInt64

NewTenantID is a helper function to return a sql.NullInt64. The ID is considered valid if greater than 0.

func RunAtMidnight Uses

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 Uses

type Analyzer struct {
    // contains filtered or unexported fields
}

Analyzer provides an interface to analyze processed data and hits.

func NewAnalyzer Uses

func NewAnalyzer(store Store, config *AnalyzerConfig) *Analyzer

NewAnalyzer returns a new Analyzer for given Store.

func (*Analyzer) ActiveVisitors Uses

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) Browser Uses

func (analyzer *Analyzer) Browser(filter *Filter) ([]BrowserStats, error)

Browser returns the visitor count per browser.

func (*Analyzer) Country Uses

func (analyzer *Analyzer) Country(filter *Filter) ([]CountryStats, error)

Country returns the visitor count per country.

func (*Analyzer) Growth Uses

func (analyzer *Analyzer) Growth(filter *Filter) (*Growth, error)

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) Languages Uses

func (analyzer *Analyzer) Languages(filter *Filter) ([]LanguageStats, error)

Languages returns the visitor count per language.

func (*Analyzer) OS Uses

func (analyzer *Analyzer) OS(filter *Filter) ([]OSStats, error)

OS returns the visitor count per operating system.

func (*Analyzer) PageBrowser Uses

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) PageLanguages Uses

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.

func (*Analyzer) PageOS Uses

func (analyzer *Analyzer) PageOS(filter *Filter) ([]OSStats, error)

PageOS returns the visitor count per operating system, day, path, and for the given time frame. The path is mandatory.

func (*Analyzer) PagePlatform Uses

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) PageReferrer Uses

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) PageVisitors Uses

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) Platform Uses

func (analyzer *Analyzer) Platform(filter *Filter) *VisitorStats

Platform returns the visitor count per browser.

func (*Analyzer) Referrer Uses

func (analyzer *Analyzer) Referrer(filter *Filter) ([]ReferrerStats, error)

Referrer returns the visitor count per referrer.

func (*Analyzer) Screen Uses

func (analyzer *Analyzer) Screen(filter *Filter) ([]ScreenStats, error)

Screen returns the visitor count per screen size (width and height).

func (*Analyzer) ScreenClass Uses

func (analyzer *Analyzer) ScreenClass(filter *Filter) ([]ScreenStats, error)

ScreenClass returns the visitor count per screen class.

func (*Analyzer) TimeOfDay Uses

func (analyzer *Analyzer) TimeOfDay(filter *Filter) ([]TimeOfDayVisitors, error)

TimeOfDay returns the visitor count per day and hour for given time frame.

func (*Analyzer) VisitorHours Uses

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!

func (*Analyzer) Visitors Uses

func (analyzer *Analyzer) Visitors(filter *Filter) ([]Stats, error)

Visitors returns the visitor count, session count, and bounce rate per day.

type AnalyzerConfig Uses

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 Uses

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 Uses

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 Uses

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 Uses

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.

func NewFilter Uses

func NewFilter(tenantID sql.NullInt64) *Filter

NewFilter returns a new default filter for given tenant and the past week.

func (*Filter) Days Uses

func (filter *Filter) Days() int

Days returns the number of days covered by the filter.

type GeoDB Uses

type GeoDB struct {
    // contains filtered or unexported fields
}

GeoDB maps IPs to their geo location based on MaxMinds GeoLite2 or GeoIP2 database.

func NewGeoDB Uses

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.

func (*GeoDB) Close Uses

func (db *GeoDB) Close() error

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!

func (*GeoDB) CountryCode Uses

func (db *GeoDB) CountryCode(ip string) string

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 Uses

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 Uses

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 Uses

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.

func HitFromRequest Uses

func HitFromRequest(r *http.Request, salt string, options *HitOptions) Hit

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.

func (Hit) String Uses

func (hit Hit) String() string

String implements the Stringer interface.

type HitOptions Uses

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 Uses

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 Uses

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 Uses

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.

type PathVisitors Uses

type PathVisitors struct {
    Path  string  `json:"path"`
    Stats []Stats `json:"stats"`
}

PathVisitors represents visitor statistics per day for a path.

type PostgresConfig Uses

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.

type PostgresStore Uses

type PostgresStore struct {
    DB *sqlx.DB
    // contains filtered or unexported fields
}

PostgresStore implements the Store interface.

func NewPostgresStore Uses

func NewPostgresStore(db *sql.DB, config *PostgresConfig) *PostgresStore

NewPostgresStore creates a new postgres storage for given database connection and logger.

func (*PostgresStore) ActivePageVisitors Uses

func (store *PostgresStore) ActivePageVisitors(tenantID sql.NullInt64, from time.Time) ([]Stats, error)

ActivePageVisitors implements the Store interface.

func (*PostgresStore) ActiveVisitors Uses

func (store *PostgresStore) ActiveVisitors(tenantID sql.NullInt64, from time.Time) int

ActiveVisitors implements the Store interface.

func (*PostgresStore) Commit Uses

func (store *PostgresStore) Commit(tx *sqlx.Tx)

Commit implements the Store interface.

func (*PostgresStore) CountVisitors Uses

func (store *PostgresStore) CountVisitors(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) *Stats

CountVisitors implements the Store interface.

func (*PostgresStore) CountVisitorsByBrowser Uses

func (store *PostgresStore) CountVisitorsByBrowser(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]BrowserStats, error)

CountVisitorsByBrowser implements the Store interface.

func (*PostgresStore) CountVisitorsByCountryCode Uses

func (store *PostgresStore) CountVisitorsByCountryCode(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]CountryStats, error)

CountVisitorsByCountryCode implements the Store interface.

func (*PostgresStore) CountVisitorsByHour Uses

func (store *PostgresStore) CountVisitorsByHour(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]VisitorTimeStats, error)

CountVisitorsByPathAndHour implements the Store interface.

func (*PostgresStore) CountVisitorsByLanguage Uses

func (store *PostgresStore) CountVisitorsByLanguage(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]LanguageStats, error)

CountVisitorsByLanguage implements the Store interface.

func (*PostgresStore) CountVisitorsByOS Uses

func (store *PostgresStore) CountVisitorsByOS(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]OSStats, error)

CountVisitorsByOS implements the Store interface.

func (*PostgresStore) CountVisitorsByPath Uses

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 (*PostgresStore) CountVisitorsByPathAndBrowser Uses

func (store *PostgresStore) CountVisitorsByPathAndBrowser(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]BrowserStats, error)

CountVisitorsByPathAndBrowser implements the Store interface.

func (*PostgresStore) CountVisitorsByPathAndLanguage Uses

func (store *PostgresStore) CountVisitorsByPathAndLanguage(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]LanguageStats, error)

CountVisitorsByPathAndLanguage implements the Store interface.

func (*PostgresStore) CountVisitorsByPathAndMaxOneHit Uses

func (store *PostgresStore) CountVisitorsByPathAndMaxOneHit(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) int

CountVisitorsByPathAndMaxOneHit implements the Store interface.

func (*PostgresStore) CountVisitorsByPathAndOS Uses

func (store *PostgresStore) CountVisitorsByPathAndOS(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]OSStats, error)

CountVisitorsByPathAndOS implements the Store interface.

func (*PostgresStore) CountVisitorsByPathAndReferrer Uses

func (store *PostgresStore) CountVisitorsByPathAndReferrer(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time, path string) ([]ReferrerStats, error)

CountVisitorsByPathAndReferrer implements the Store interface.

func (*PostgresStore) CountVisitorsByPlatform Uses

func (store *PostgresStore) CountVisitorsByPlatform(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) *VisitorStats

CountVisitorsByPlatform implements the Store interface.

func (*PostgresStore) CountVisitorsByReferrer Uses

func (store *PostgresStore) CountVisitorsByReferrer(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]ReferrerStats, error)

CountVisitorsByReferrer implements the Store interface.

func (*PostgresStore) CountVisitorsByScreenClass Uses

func (store *PostgresStore) CountVisitorsByScreenClass(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]ScreenStats, error)

CountVisitorsByScreenClass implements the Store interface.

func (*PostgresStore) CountVisitorsByScreenSize Uses

func (store *PostgresStore) CountVisitorsByScreenSize(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) ([]ScreenStats, error)

CountVisitorsByScreenSize implements the Store interface.

func (*PostgresStore) DeleteHitsByDay Uses

func (store *PostgresStore) DeleteHitsByDay(tx *sqlx.Tx, tenantID sql.NullInt64, day time.Time) error

DeleteHitsByDay implements the Store interface.

func (*PostgresStore) HitDays Uses

func (store *PostgresStore) HitDays(tenantID sql.NullInt64) ([]time.Time, error)

HitDays implements the Store interface.

func (*PostgresStore) HitPaths Uses

func (store *PostgresStore) HitPaths(tenantID sql.NullInt64, day time.Time) ([]string, error)

HitPaths implements the Store interface.

func (*PostgresStore) NewTx Uses

func (store *PostgresStore) NewTx() *sqlx.Tx

NewTx implements the Store interface.

func (*PostgresStore) PageBrowser Uses

func (store *PostgresStore) PageBrowser(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]BrowserStats, error)

PageBrowser implements the Store interface.

func (*PostgresStore) PageLanguages Uses

func (store *PostgresStore) PageLanguages(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]LanguageStats, error)

PageLanguages implements the Store interface.

func (*PostgresStore) PageOS Uses

func (store *PostgresStore) PageOS(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]OSStats, error)

PageOS implements the Store interface.

func (*PostgresStore) PagePlatform Uses

func (store *PostgresStore) PagePlatform(tenantID sql.NullInt64, path string, from time.Time, to time.Time) *VisitorStats

PagePlatform implements the Store interface.

func (*PostgresStore) PageReferrer Uses

func (store *PostgresStore) PageReferrer(tenantID sql.NullInt64, path string, from time.Time, to time.Time) ([]ReferrerStats, error)

PageReferrer implements the Store interface.

func (*PostgresStore) PageVisitors Uses

func (store *PostgresStore) PageVisitors(tenantID sql.NullInt64, path string, from, to time.Time) ([]Stats, error)

PageVisitors implements the Store interface.

func (*PostgresStore) Paths Uses

func (store *PostgresStore) Paths(tenantID sql.NullInt64, from, to time.Time) ([]string, error)

Paths implements the Store interface.

func (*PostgresStore) Rollback Uses

func (store *PostgresStore) Rollback(tx *sqlx.Tx)

Rollback implements the Store interface.

func (*PostgresStore) SaveBrowserStats Uses

func (store *PostgresStore) SaveBrowserStats(tx *sqlx.Tx, entity *BrowserStats) error

SaveBrowserStats implements the Store interface.

func (*PostgresStore) SaveCountryStats Uses

func (store *PostgresStore) SaveCountryStats(tx *sqlx.Tx, entity *CountryStats) error

SaveCountryStats implements the Store interface.

func (*PostgresStore) SaveHits Uses

func (store *PostgresStore) SaveHits(hits []Hit) error

SaveHits implements the Store interface.

func (*PostgresStore) SaveLanguageStats Uses

func (store *PostgresStore) SaveLanguageStats(tx *sqlx.Tx, entity *LanguageStats) error

SaveLanguageStats implements the Store interface.

func (*PostgresStore) SaveOSStats Uses

func (store *PostgresStore) SaveOSStats(tx *sqlx.Tx, entity *OSStats) error

SaveOSStats implements the Store interface.

func (*PostgresStore) SaveReferrerStats Uses

func (store *PostgresStore) SaveReferrerStats(tx *sqlx.Tx, entity *ReferrerStats) error

SaveReferrerStats implements the Store interface.

func (*PostgresStore) SaveScreenStats Uses

func (store *PostgresStore) SaveScreenStats(tx *sqlx.Tx, entity *ScreenStats) error

SaveScreenStats implements the Store interface.

func (*PostgresStore) SaveVisitorStats Uses

func (store *PostgresStore) SaveVisitorStats(tx *sqlx.Tx, entity *VisitorStats) error

SaveVisitorStats implements the Store interface.

func (*PostgresStore) SaveVisitorTimeStats Uses

func (store *PostgresStore) SaveVisitorTimeStats(tx *sqlx.Tx, entity *VisitorTimeStats) error

SaveVisitorTimeStats implements the Store interface.

func (*PostgresStore) Session Uses

func (store *PostgresStore) Session(tenantID sql.NullInt64, fingerprint string, maxAge time.Time) time.Time

Session implements the Store interface.

func (*PostgresStore) VisitorBrowser Uses

func (store *PostgresStore) VisitorBrowser(tenantID sql.NullInt64, from, to time.Time) ([]BrowserStats, error)

VisitorBrowser implements the Store interface.

func (*PostgresStore) VisitorCountry Uses

func (store *PostgresStore) VisitorCountry(tenantID sql.NullInt64, from, to time.Time) ([]CountryStats, error)

VisitorCountry implements the Store interface.

func (*PostgresStore) VisitorHours Uses

func (store *PostgresStore) VisitorHours(tenantID sql.NullInt64, from time.Time, to time.Time) ([]VisitorTimeStats, error)

VisitorHours implements the Store interface.

func (*PostgresStore) VisitorLanguages Uses

func (store *PostgresStore) VisitorLanguages(tenantID sql.NullInt64, from, to time.Time) ([]LanguageStats, error)

VisitorLanguages implements the Store interface.

func (*PostgresStore) VisitorOS Uses

func (store *PostgresStore) VisitorOS(tenantID sql.NullInt64, from, to time.Time) ([]OSStats, error)

VisitorOS implements the Store interface.

func (*PostgresStore) VisitorPlatform Uses

func (store *PostgresStore) VisitorPlatform(tenantID sql.NullInt64, from, to time.Time) *VisitorStats

VisitorPlatform implements the Store interface.

func (*PostgresStore) VisitorReferrer Uses

func (store *PostgresStore) VisitorReferrer(tenantID sql.NullInt64, from, to time.Time) ([]ReferrerStats, error)

VisitorReferrer implements the Store interface.

func (*PostgresStore) VisitorScreenClass Uses

func (store *PostgresStore) VisitorScreenClass(tenantID sql.NullInt64, from, to time.Time) ([]ScreenStats, error)

VisitorScreenClass implements the Store interface.

func (*PostgresStore) VisitorScreenSize Uses

func (store *PostgresStore) VisitorScreenSize(tenantID sql.NullInt64, from, to time.Time) ([]ScreenStats, error)

VisitorScreenSize implements the Store interface.

func (*PostgresStore) Visitors Uses

func (store *PostgresStore) Visitors(tenantID sql.NullInt64, from, to time.Time) ([]Stats, error)

Visitors implements the Store interface.

func (*PostgresStore) VisitorsSum Uses

func (store *PostgresStore) VisitorsSum(tenantID sql.NullInt64, from, to time.Time, path string) (*Stats, error)

VisitorsSum implements the Store interface.

type Processor Uses

type Processor struct {
    // contains filtered or unexported fields
}

Processor processes hits to reduce them into meaningful statistics.

func NewProcessor Uses

func NewProcessor(store Store) *Processor

NewProcessor creates a new Processor for given Store.

func (*Processor) Process Uses

func (processor *Processor) Process() error

Process processes all hits in database and deletes them afterwards.

func (*Processor) ProcessTenant Uses

func (processor *Processor) ProcessTenant(tenantID sql.NullInt64) error

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 Uses

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 Uses

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 Uses

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.

func (*Stats) GetID Uses

func (stats *Stats) GetID() int64

GetID returns the ID.

func (*Stats) GetVisitors Uses

func (stats *Stats) GetVisitors() int

GetVisitors returns the visitor count.

type Store Uses

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 Uses

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 Uses

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 Uses

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.

func (*Tracker) Flush Uses

func (tracker *Tracker) Flush()

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) Hit Uses

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.

func (*Tracker) SetGeoDB Uses

func (tracker *Tracker) SetGeoDB(geoDB *GeoDB)

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.

func (*Tracker) Stop Uses

func (tracker *Tracker) Stop()

Stop flushes and stops all workers.

type TrackerConfig Uses

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 Uses

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.

func ParseUserAgent Uses

func ParseUserAgent(ua string) UserAgent

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.

func (*UserAgent) IsDesktop Uses

func (ua *UserAgent) IsDesktop() bool

IsDesktop returns true if the user agent is a desktop device.

func (*UserAgent) IsMobile Uses

func (ua *UserAgent) IsMobile() bool

IsMobile returns true if the user agent is a mobile device.

type VisitorStats Uses

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.

type VisitorTimeStats Uses

type VisitorTimeStats struct {
    Stats

    Hour int `db:"hour" json:"hour"`
}

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.