gpt4 book ai didi

google-analytics - 谷歌分析 API : How to use Custom Variables in the code?

转载 作者:行者123 更新时间:2023-12-02 14:38:28 25 4
gpt4 key购买 nike

尝试模仿我在 Google Analytics(分析)仪表板上的操作方式。 enter image description here

如何使用自定义变量值创建分段?我尝试编写以下代码,但它没有返回任何结果。
可以将自定义变量值设置为DimensionFilter吗?

// Create Dimension Filter.
SegmentDimensionFilter dimensionFilter = new SegmentDimensionFilter()
.setDimensionName("ga:customVarValue1")
.setOperator("EXACT")
.setExpressions(Arrays.asList("account_id"));


// Create Segment Filter Clause.
SegmentFilterClause segmentFilterClause = new SegmentFilterClause()
.setDimensionFilter(dimensionFilter);

// Create the Or Filters for Segment.
OrFiltersForSegment orFiltersForSegment = new OrFiltersForSegment()
.setSegmentFilterClauses(Arrays.asList(segmentFilterClause));

// Create the Simple Segment.
SimpleSegment simpleSegment = new SimpleSegment()
.setOrFiltersForSegment(Arrays.asList(orFiltersForSegment));

// Create the Segment Filters.
SegmentFilter segmentFilter = new SegmentFilter()
.setSimpleSegment(simpleSegment);

// Create the Segment Definition.
SegmentDefinition segmentDefinition = new SegmentDefinition()
.setSegmentFilters(Arrays.asList(segmentFilter));

// Create the Dynamic Segment.
DynamicSegment dynamicSegment = new DynamicSegment()
.setSessionSegment(segmentDefinition)
.setName("Sessions with Safari browser");

// Create the Segments object.
Segment segment = new Segment()
.setDynamicSegment(dynamicSegment);

// Create the ReportRequest object.
ReportRequest request = new ReportRequest()
.setViewId(VIEW_ID)
.setDateRanges(Arrays.asList(dateRange))
.setDimensions(Arrays.asList(segmentDimensions))
.setSegments(Arrays.asList(segment))
.setMetrics(Arrays.asList(sessions));

最佳答案

从长远来看,有一些事情可以帮助您。

  1. 不再使用自定义变量,因为它们被替换为 custom dimensions对于 Universal Analytics .
  2. 使用API explorer构建并共享您的调试请求。
  3. 最初查询时不使用过滤器或分段,以确保您要查找的数据存在于您正在查询的 View 中。

也就是说,创建一个带有自定义变量分段的请求:

Java 示例

  // Create the DateRange object.
DateRange dateRange = new DateRange();
dateRange.setStartDate("2015-06-15");
dateRange.setEndDate("2015-06-30");

// Create the Metrics object.
Metric sessions = new Metric()
.setExpression("ga:sessions")
.setAlias("sessions");

//Create the custom var dimension.
Dimension customVarValue1 = new Dimension()
.setName("ga:customVarValue1");

// Create the segment dimension.
Dimension segmentDimensions = new Dimension()
.setName("ga:customVarValue1");

// Create Dimension Filter.
SegmentDimensionFilter dimensionFilter = new SegmentDimensionFilter()
.setDimensionName("ga:customVarValue1")
.setOperator("PARTIAL")
.setExpressions(Arrays.asList("account_id"));

// Create Segment Filter Clause.
SegmentFilterClause segmentFilterClause = new SegmentFilterClause()
.setDimensionFilter(dimensionFilter);

// Create the Or Filters for Segment.
OrFiltersForSegment orFiltersForSegment = new OrFiltersForSegment()
.setSegmentFilterClauses(Arrays.asList(segmentFilterClause));

// Create the Simple Segment.
SimpleSegment simpleSegment = new SimpleSegment()
.setOrFiltersForSegment(Arrays.asList(orFiltersForSegment));

// Create the Segment Filters.
SegmentFilter segmentFilter = new SegmentFilter()
.setSimpleSegment(simpleSegment);

// Create the Segment Definition.
SegmentDefinition segmentDefinition = new SegmentDefinition()
.setSegmentFilters(Arrays.asList(segmentFilter));

// Create the Dynamic Segment.
DynamicSegment dynamicSegment = new DynamicSegment()
.setSessionSegment(segmentDefinition)
.setName("Sessions with Safari browser");

// Create the Segments object.
Segment segment = new Segment()
.setDynamicSegment(dynamicSegment);

// Create the ReportRequest object.
ReportRequest request = new ReportRequest()
.setViewId("XXXX")
.setDateRanges(Arrays.asList(dateRange))
.setDimensions(Arrays.asList(customVarValue1, segmentDimensions))
.setSegments(Arrays.asList(segment))
.setMetrics(Arrays.asList(sessions));

// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest()
.setReportRequests(Arrays.asList(request));

// Call the batchGet method.
GetReportsResponse response = analyticsreporting.reports().batchGet(getReport)
.execute();

要检查的事情:

  1. 确保您使用正确的自定义变量。您是要搜索 ga:customVarName1 吗?
  2. 查询不带分段或过滤器的变量,以确保数据在过滤/分段之前存在。
  3. 确保您使用正确的 operator .

最后,如果您在 UI 中创建分段,则始终可以通过 API 中的分段 ID 进行查询:

按段 ID 进行 Java 请求。

  // Create the Segments object for returning users.
Segment segment = new Segment()
.setSegmentId("gaid::-3");

// Create the ReportRequest object.
ReportRequest request = new ReportRequest()
.setViewId("XXXX")
.setDateRanges(Arrays.asList(dateRange))
.setDimensions(Arrays.asList(browser, segmentDimensions))
.setSegments(Arrays.asList(segment))
.setMetrics(Arrays.asList(sessions));

// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest()
.setReportRequests(Arrays.asList(request));

要获取分段 ID,您可以调用 segments.list Analytics Management API 中的方法。

关于google-analytics - 谷歌分析 API : How to use Custom Variables in the code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37596951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com