diff --git a/go.mod b/go.mod index 498e62c..0d346a1 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/google/uuid v1.5.0 github.com/joho/godotenv v1.5.1 github.com/oschwald/geoip2-golang v1.9.0 + github.com/posthog/posthog-go v1.2.1 github.com/samber/lo v1.39.0 github.com/stretchr/testify v1.8.4 github.com/swaggo/swag v1.16.2 diff --git a/go.sum b/go.sum index cc8db3c..7f87636 100644 --- a/go.sum +++ b/go.sum @@ -1626,6 +1626,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/posthog/posthog-go v1.2.1 h1:6OwNJDRekQHi/4JmJJsxb7+tB+6UXRtVNntrCd/7Y6g= +github.com/posthog/posthog-go v1.2.1/go.mod h1:QjlpryJtfYLrZF2GUkAhejH4E7WlDbdKkvOi5hLmkdg= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= github.com/pquerna/cachecontrol v0.1.0/go.mod h1:NrUG3Z7Rdu85UNR3vm7SOsl1nFIeSiQnrHV5K9mBcUI= diff --git a/onepixel.local.env b/onepixel.local.env index 765c997..c1535c4 100644 --- a/onepixel.local.env +++ b/onepixel.local.env @@ -17,4 +17,6 @@ JWT_DURATION_DAYS=90 EVENTDB_DIALECT=duckdb EVENTDB_URL=events.db -# EVENTDB_URL="clickhouse://clickhouse:clickhouse@127.0.0.1:9000/onepixel?dial_timeout=10s&read_timeout=20s" \ No newline at end of file +# EVENTDB_URL="clickhouse://clickhouse:clickhouse@127.0.0.1:9000/onepixel?dial_timeout=10s&read_timeout=20s" + +POSTHOG_API_KEY="your-posthog-api-key-here" diff --git a/onepixel.production.env b/onepixel.production.env index 307b256..2cf0559 100644 --- a/onepixel.production.env +++ b/onepixel.production.env @@ -2,4 +2,5 @@ ADMIN_SITE_HOST=onepixel.link MAIN_SITE_HOST=1px.li DB_DIALECT=postgres EVENTDB_DIALECT=clickhouse -USE_FILE_DB=false \ No newline at end of file +USE_FILE_DB=false +POSTHOG_API_KEY="your-posthog-api-key-here" diff --git a/src/config/vars.go b/src/config/vars.go index f23f9cf..74c7883 100644 --- a/src/config/vars.go +++ b/src/config/vars.go @@ -28,6 +28,8 @@ var AdminUserEmail string var JwtSigningKey string var JwtDurationDays int +var PosthogApiKey string + // should run after env.go#init as this `vars` is alphabetically after `env` func init() { Env, _ = lo.Coalesce( @@ -58,4 +60,5 @@ func init() { AdminUserEmail = os.Getenv("ADMIN_USER_EMAIL") JwtSigningKey = os.Getenv("JWT_SIGNING_KEY") JwtDurationDays, _ = strconv.Atoi(os.Getenv("JWT_DURATION_DAYS")) + PosthogApiKey = os.Getenv("POSTHOG_API_KEY") } diff --git a/src/controllers/events.go b/src/controllers/events.go index a52aeb4..2c191ab 100644 --- a/src/controllers/events.go +++ b/src/controllers/events.go @@ -6,22 +6,26 @@ import ( "github.com/oschwald/geoip2-golang" "github.com/samber/lo" "gorm.io/gorm" + "onepixel_backend/src/config" "onepixel_backend/src/db" "onepixel_backend/src/db/models" "onepixel_backend/src/utils/applogger" "onepixel_backend/src/utils/clientinfo" + "github.com/posthog/posthog-go" ) type EventsController struct { // event logging eventDb (not the main app eventDb) eventDb *gorm.DB geoipDB *geoip2.Reader + posthogClient posthog.Client } func CreateEventsController() *EventsController { return &EventsController{ eventDb: db.GetEventsDB(), geoipDB: db.GetGeoIPDB(), + posthogClient: posthog.New(config.PosthogApiKey), } } @@ -86,6 +90,16 @@ func (c *EventsController) LogRedirectAsync(redirData *EventRedirectData) { } return tx.Error }) + // Log event to Posthog + c.posthogClient.Enqueue(posthog.Capture{ + DistinctId: redirData.IPAddress, + Event: "$pageview", + Properties: posthog.NewProperties(). + Set("path", redirData.ShortURL). + Set("user_agent", redirData.UserAgent). + Set("referer", redirData.Referer), + }) + return event.ID }) diff --git a/src/main.go b/src/main.go index e5a2cfa..323988c 100644 --- a/src/main.go +++ b/src/main.go @@ -15,6 +15,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/samber/lo" + "github.com/posthog/posthog-go" ) func main() { @@ -23,6 +24,10 @@ func main() { eventDb := db.GetEventsDB() geoipDb := db.GetGeoIPDB() + // Initialize Posthog client + posthogClient := posthog.New(config.PosthogApiKey) + defer posthogClient.Close() + // Create the app adminApp := server.CreateAdminApp() mainApp := server.CreateMainApp() diff --git a/src/routes/redirect/redirect.go b/src/routes/redirect/redirect.go index 1ab471d..bfbbf41 100644 --- a/src/routes/redirect/redirect.go +++ b/src/routes/redirect/redirect.go @@ -8,6 +8,7 @@ import ( "onepixel_backend/src/server/validators" "onepixel_backend/src/utils/applogger" "strings" + "github.com/posthog/posthog-go" ) var urlsController *controllers.UrlsController @@ -49,6 +50,17 @@ func redirectShortCode(ctx *fiber.Ctx) error { UserAgent: ctx.Get("User-Agent"), Referer: ctx.Get("Referer"), }) + // Log event to Posthog + client := posthog.New("your-posthog-api-key-here") + defer client.Close() + client.Enqueue(posthog.Capture{ + DistinctId: strings.Split(ctx.Get("X-Forwarded-For"), ",")[0], + Event: "$pageview", + Properties: posthog.NewProperties(). + Set("path", url.ShortURL). + Set("user_agent", ctx.Get("User-Agent")). + Set("referer", ctx.Get("Referer")), + }) // cache for 1 min only ctx.Response().Header.Set("Cache-Control", "public, max-age=60") return ctx.Redirect(url.LongURL, fiber.StatusMovedPermanently)