gopiwik/piwik/handler.go

120 lines
3.9 KiB
Go
Raw Permalink Normal View History

2017-03-03 22:07:39 +01:00
package piwik
import (
2021-01-06 18:22:21 +01:00
"encoding/json"
2017-03-03 22:07:39 +01:00
"log"
"net/http"
"os"
)
type PiwikHandler struct {
fileServer http.Handler
siteStorage SiteStorageInterface
visitStorage VisitStorageInterface
}
func NewPiwikHandler(ss SiteStorageInterface, vs VisitStorageInterface) *PiwikHandler {
return &PiwikHandler{
fileServer: http.FileServer(http.Dir("assets")),
siteStorage: ss,
visitStorage: vs,
}
}
func (h *PiwikHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
switch r.URL.Path {
case "/piwik.php":
h.handlePiwik(w, r)
default:
h.fileServer.ServeHTTP(w, r)
}
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
func (h *PiwikHandler) handlePiwik(w http.ResponseWriter, r *http.Request) {
pr := ParseV3(r)
v := &Visit{
SiteID: pr.SiteID,
URL: pr.URL,
ActionName: pr.ActionName,
VisitorID: pr.VisitorID,
Referrer: pr.Referrer,
CustomVars: pr.CustomVars,
VisitsCount: pr.VisitsCount,
ViewTime: pr.ViewTime,
VisitorFirstVisitTime: pr.VisitorFirstVisitTime,
CampaignName: pr.CampaignName,
CampaignKeyword: pr.CampaignKeyword,
Resolution: pr.Resolution,
LocalHour: pr.LocalHour,
LocalMinute: pr.LocalMinute,
LocalSeconds: pr.LocalSeconds,
LocalTimeUTC: pr.LocalTimeUTC,
Plugins: pr.Plugins,
SupportsCookies: pr.SupportsCookies,
UserAgent: pr.UserAgent,
Language: pr.Language,
UserID: pr.UserID,
CustomUserID: pr.CustomUserID,
NewVisit: pr.NewVisit,
PageCustomVars: pr.PageCustomVars,
Link: pr.Link,
Download: pr.Download,
Search: pr.Search,
SearchCategory: pr.SearchCategory,
SearchCount: pr.SearchCount,
PageViewID: pr.PageViewID,
GoalID: pr.GoalID,
Revenue: pr.Revenue,
GenerationTime: pr.GenerationTime,
Characterset: pr.Characterset,
EventCategory: pr.EventCategory,
EventAction: pr.EventAction,
EventName: pr.EventName,
EventValue: pr.EventValue,
ContentName: pr.ContentName,
ContentPiece: pr.ContentPiece,
ContentTarget: pr.ContentTarget,
ContentInteraction: pr.ContentInteraction,
ECommerceID: pr.ECommerceID,
ECommerceItems: pr.ECommerceItems,
ECommerceSubTotal: pr.ECommerceSubTotal,
ECommerceTax: pr.ECommerceTax,
ECommerceShipping: pr.ECommerceShipping,
ECommerceDiscount: pr.ECommerceDiscount,
ECommerceTime: pr.ECommerceTime,
TokenAuth: pr.TokenAuth,
CustomIP: pr.CustomIP,
CustomDateTime: pr.CustomDateTime,
CustomCountry: pr.CustomCountry,
CustomRegion: pr.CustomRegion,
CustomCity: pr.CustomCity,
CustomLatitude: pr.CustomLatitude,
CustomLongitude: pr.CustomLongitude,
MediaID: pr.MediaID,
MediaTitle: pr.MediaTitle,
MediaResource: pr.MediaResource,
MediaType: pr.MediaType,
MediaPlayerName: pr.MediaPlayerName,
MediaSecondsPlayingTime: pr.MediaSecondsPlayingTime,
MediaLength: pr.MediaLength,
MediaProgress: pr.MediaProgress,
MediaTimeUntilPlay: pr.MediaTimeUntilPlay,
MediaWidth: pr.MediaWidth,
MediaHeight: pr.MediaHeight,
MediaFullscreen: pr.MediaFullscreen,
}
if e := h.visitStorage.Add(v); e != nil {
log.Println(e)
}
if pr.SendImage == "0" {
w.WriteHeader(204)
}
json.NewEncoder(os.Stdout).Encode(v)
}