package main
import (
"context"
"fmt"
"log"
"os"
dub "github.com/dubinc/dub-go"
)
func main() {
// Initialize the Dub SDK with your API key
d := dub.New(
dub.WithSecurity(os.Getenv("DUB_API_KEY")), // optional, defaults to DUB_API_KEY
)
// Create a new link
request := &operations.CreateLinkRequestBody{
URL: "https://google.com",
}
ctx := context.Background()
res, err := d.Links.Create(ctx, request)
if err != nil {
log.Fatal(err)
}
if res.LinkSchema != nil {
fmt.Println(res.LinkSchema.ShortLink) // e.g. https://dub.sh/abc123
}
// Get analytics for the link
analyticsRequest := operations.RetrieveAnalyticsRequest{
LinkId: res.LinkSchema.ID,
GroupBy: "timeseries",
Interval: "30d",
}
analyticsRes, err := d.Analytics.Retrieve(ctx, analyticsRequest)
if err != nil {
log.Fatal(err)
}
if analyticsRes.OneOf != nil {
fmt.Println(analyticsRes.OneOf) // e.g. [{ start: "2024-01-01", clicks: 100 }]
}
}