使用 GA4 API 获取所有事件参数值和事件计数

问题描述 投票:0回答:1

我目前有一个自定义事件,其事件参数名为“term”。此事件参数的值是用户在搜索栏上键入的任何内容。

我想以编程方式获取有关用户搜索过的所有值以及这些术语被搜索了多少次的报告数据。就像这样graph

我正在尝试使用 GA4 api https://developers.google.com/analytics/devguides/reporting/data/v1 来生成此报告,但我无法获取我想要的信息。

我尝试创建具有事件范围的自定义维度,并将“term”设置为事件参数。然而,当我尝试运行报告时,它是空的

async function runReport() {
  const [response] = await analyticsDataClient.runReport({
    property: `properties/${propertyId}`,
    dateRanges: [
      {
        startDate: '2021-03-31',
        endDate: 'today',
      },
    ],
    dimensions: [
      {
        name: 'customEvent:term',
      },
    ],
    metrics: [
      {
        name: 'eventCount',
      },
    ],
  });

  console.log('Report result:');
  response.rows.forEach(row => {
    console.log(row.dimensionValues[0], row.metricValues[0]);
  });
}

关于如何获得我想要的报告还有其他想法吗?

提前致谢

google-analytics-api google-analytics-4
1个回答
0
投票

我假设你已经成功创建了自定义事件和自定义参数或自定义维度,下面的代码是用java编写的:

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
import com.google.analytics.data.v1beta.DateRange;
import com.google.analytics.data.v1beta.Dimension;
import com.google.analytics.data.v1beta.Metric;
import com.google.analytics.data.v1beta.Row;
import com.google.analytics.data.v1beta.RunReportRequest;
import com.google.analytics.data.v1beta.RunReportResponse;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import io.grpc.LoadBalancerRegistry;
import io.grpc.grpclb.GrpclbLoadBalancerProvider;

public class test {

    static {
        LoadBalancerRegistry.getDefaultRegistry().register( new GrpclbLoadBalancerProvider());
    }
    public static void main(String[] args) throws IOException, SQLException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar  = Calendar.getInstance();

        calendar.add(Calendar.DAY_OF_MONTH, -2);
        String two_daysAgo=dateFormat.format(calendar.getTime());    

        GoogleCredentials credentials =
                GoogleCredentials.fromStream(new FileInputStream("path to your credentials"));

        BetaAnalyticsDataSettings betaAnalyticsDataSettings =
                BetaAnalyticsDataSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                .build();    

        try (BetaAnalyticsDataClient analyticsData =
                BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {

            RunReportRequest request =
                    RunReportRequest.newBuilder()
                    .setProperty("properties/" + "your property Id")
                    .addDateRanges(DateRange.newBuilder().setStartDate("your start time").setEndDate(two_daysAgo))


                    .addDimensions(Dimension.newBuilder().setName("date")) 
                    .addDimensions(Dimension.newBuilder().setName("eventName"))
                    .addDimensions(Dimension.newBuilder().setName("customEvent:page_location")) 
                    .addDimensions(Dimension.newBuilder().setName("customEvent:your_custom_event_name"))

                    .addMetrics(Metric.newBuilder().setName("eventCount"))
                    .addMetrics(Metric.newBuilder().setName("eventsPerSession"))
                    .addMetrics(Metric.newBuilder().setName("eventValue"))

                    .build();
            RunReportResponse response = analyticsData.runReport(request);

            for(Row row:response.getRowsList()) {
                System.out.println("\n\n\n\n\n row is " + row.toString());
            }


        }

    }


}

我个人使用 48 小时前的结束日期,因为我更喜欢确保所有内容都已更新。

© www.soinside.com 2019 - 2024. All rights reserved.