Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log redirect events to Posthog #64

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 3 additions & 1 deletion onepixel.local.env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
# 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"
3 changes: 2 additions & 1 deletion onepixel.production.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ ADMIN_SITE_HOST=onepixel.link
MAIN_SITE_HOST=1px.li
DB_DIALECT=postgres
EVENTDB_DIALECT=clickhouse
USE_FILE_DB=false
USE_FILE_DB=false
POSTHOG_API_KEY="your-posthog-api-key-here"
3 changes: 3 additions & 0 deletions src/config/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")
}
14 changes: 14 additions & 0 deletions src/controllers/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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
})

Expand Down
5 changes: 5 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"github.com/posthog/posthog-go"
)

func main() {
Expand All @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions src/routes/redirect/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading