'how to use Google Analytics Data API v1 with golang

there is no golang demo code to use Google Analytics Data API v1 in https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries

when I write golang code to use Analytics Data API v1, there occurs an error like below:

C:\Users\qiuskill\go\src\google_api_test>go run main.go
&{0xc0001ac700}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x1114c1d]

goroutine 1 [running]:
google.golang.org/grpc/internal/grpcsync.(*Event).HasFired(...)
        C:/Users/qiuskill/go/pkg/mod/google.golang.org/[email protected]/internal/grpcsync/event.go:55
google.golang.org/grpc.(*ClientConn).waitForResolvedAddrs(0xc00011bd20, {0x12b3670, 0xc000022050})
        C:/Users/qiuskill/go/pkg/mod/google.golang.org/[email protected]/clientconn.go:573 +0x3d
google.golang.org/grpc.newClientStream({0x12b3670, 0xc000022050}, 0x15b3a60, 0xc0001ac700, {0x1237a5c, 0xa}, {0x0, 0x0, 0x0})
        C:/Users/qiuskill/go/pkg/mod/google.golang.org/[email protected]/stream.go:177 +0x133
google.golang.org/grpc.invoke({0x12b3670, 0xc000022050}, {0x1237a5c, 0xe0}, {0x1207460, 0xc0001e6000}, {0x11ffe20, 0xc0001e8000}, 0x0, {0x0, ...})
        C:/Users/qiuskill/go/pkg/mod/google.golang.org/[email protected]/call.go:66 +0x7d
google.golang.org/grpc.(*ClientConn).Invoke(0x20aa1a80108, {0x12b3670, 0xc000022050}, {0x1237a5c, 0x0}, {0x1207460, 0xc0001e6000}, {0x11ffe20, 0xc0001e8000}, {0x0, ...})
        C:/Users/qiuskill/go/pkg/mod/google.golang.org/[email protected]/call.go:37 +0x265
google.golang.org/genproto/googleapis/analytics/data/v1beta.(*betaAnalyticsDataClient).RunReport(0xc00005ba50, {0x12b3670, 0xc000022050}, 0x1, {0x0, 0x0, 0x0})
        C:/Users/qiuskill/go/pkg/mod/google.golang.org/[email protected]/googleapis/analytics/data/v1beta/analytics_data_api.pb.go:2407 +0xce
main.main()
        C:/Users/qiuskill/go/src/google_api_test/main.go:76 +0x28d
exit status 2

below is my source code, I use ******* replaced my property ID:

package main

import (
    "context"
    "fmt"

    data "google.golang.org/genproto/googleapis/analytics/data/v1beta"
    "google.golang.org/grpc"
)

func main() {
    cc := &grpc.ClientConn{}
    // fmt.Println(cc)

    client := data.NewBetaAnalyticsDataClient(cc)
    fmt.Println(client)

    ctx := context.Background()

    runReportRequest := &data.RunReportRequest{
        Property: "properties/*******",
        DateRanges: []*data.DateRange{
            {StartDate: "2022-01-10", EndDate: "2022-01-10"},
        },
        Dimensions: []*data.Dimension{
            {Name: "city"},
        },
        Metrics: []*data.Metric{
            {Name: "activeUsers"},
        },
    }
    // fmt.Println(runReportRequest)
    _, err := client.RunReport(ctx, runReportRequest)
    if err != nil {
        panic(err)
    } else {
        // cnt := response.RowCount
        // println(string(cnt))
    }
}

Can anybody know what was the reason why there occurs an error or can anybody can write a golang demo to use Google Analytics Data API v1,wait for your answers, Thanks a lot!!!



Solution 1:[1]

This is a working example:

package main

import (
    "context"
    "fmt"
    "github.com/labstack/echo/v4"
    ga "google.golang.org/api/analyticsdata/v1beta"
    "google.golang.org/api/option"
    "net/http"
)

func main() {
    ctx := context.Background()
    client, err := ga.NewService(ctx, option.WithCredentialsFile("client.json"))

    if err != nil {
        panic(err)
    }

    runReportRequest := &ga.RunReportRequest{
        DateRanges: []*ga.DateRange{
            {
                StartDate: "2022-04-19",
                EndDate: "today"
            },
        },
        //Dimensions: []*data.Dimension{
        //    {
        //        Name: "city"
        //    },
        //},
        Metrics: []*ga.Metric{
            {
                Name: "activeUsers"
            },
        },
    }

    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        r, _ := client.Properties.RunReport("properties/xxxxxxx", runReportRequest).Do()

        fmt.Println(r)

        return c.JSON(http.StatusOK, r.Rows)
    })
    e.Logger.Fatal(e.Start(":1323"))
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P