Trying to emulate how I do it on the Google Analytics Dashboard.
How a Segment can be created with a Custom Variable Value
? I have tried writing following code but it does not return any result back.
Can Custom Variable Value
set as a 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));
A few things to help you in the long run.
- Move away from custom variables as they were replaced with custom dimensions for Universal Analytics.
- Use the API explorer to build and share your request for debugging.
- Initially query without a filter or a segment to ensure the data you are looking for is present in the View you are querying.
With that said to create a request with a segment on a custom variable:
Java Example
// 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();
Things to check.:
- Ensure that you are using the correct custom variable. Did you mean to search for
ga:customVarName1
?
- Query for the variable without the segment or filter to ensure the data is present before filtering/segmenting.
- Ensure that you are using the right operator.
Finally if you create a segment in the UI you can always query by segment ID in the API:
Java request by segment 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));
To get the segment id you can call the segments.list method in the Analytics Management API.