LoginSignup
0

More than 3 years have passed since last update.

Goでgoogle analytics apiからpv数取りたい

Posted at

Detail

記事は多いけどちょうどやりたい事の記事がなかったので、サクッと
analyticsの準備とか、パッケージの導入は他の記事見て下さい

TL;DR

// 本番運用ではSSMとかから取る
key, _ := ioutil.ReadFile("secret.json")

jwtConf, err := google.JWTConfigFromJSON(
    key,
    // スコープ(Readonly以外見た事ない)
    analytics.AnalyticsReadonlyScope,
)
if err != nil {
}

httpClient := jwtConf.Client(oauth2.NoContext)
svc, err := analytics.New(httpClient)
if err != nil {
}

res, err := svc.Data.Ga.Get("ga:viewId入れてね", "いつから", "いつまで","ga:pageviews").Dimensions("ga:pagePath").Sort("-ga:pageviews").MaxResults(最大取得件数).SamplingLevel("HIGHER_PRECISION").Do()

// resに全部入ってるよ

ハマるのはGa.Get()からのチェーンぐらいですね!
https://developers.google.com/analytics/devguides/reporting/core/v3/reference
ここ参考にすれば良いです!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0