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.
With that said to create a request with a segment on a custom variable:
Java Example
Things to check.:
ga:customVarName1
?Finally if you create a segment in the UI you can always query by segment ID in the API:
Java request by segment ID.
To get the segment id you can call the segments.list method in the Analytics Management API.