尝试模拟我在Google Analytics信息中心上执行此操作的方式。
如何使用Custom Variable Value
创建细分?我曾尝试编写以下代码,但它不会返回任何结果。
可以Custom Variable Value
设置为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));
答案 0 :(得分:1)
从长远来看,有一些事情可以帮助你。
据说用自定义变量创建一个段请求:
// 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();
要检查的事情。:
ga:customVarName1
吗?最后,如果您在UI中创建细分,则始终可以按API中的细分ID进行查询:
// 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,您可以在Google Analytics管理API中调用segments.list方法。