Installation

go get github.com/dubinc/dub-go

Basic Usage

Here’s how you can use the Dub Go SDK to create a link and retrieve click analytics in timeseries format for it:

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 }]
	}
}

For more usage examples:

  1. Organizing links by external ID, tenant ID, tags, etc
  2. Bulk link operations (create, update, delete)
  3. Retrieving link analytics

You can also check out the Go SDK quickstart for a basic example.

Additional Resources