https://developers.google.com/analytics/devguides/reporting/core/v4/samples for find data from google analytic.
I want to find data using multiple dimension filter but not able to do it.
i am using the code.
function buildSimpleSegment($segmentName, $dimension, $dimensionFilterExpression) {
// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");
// Create Dimension Filter.
$dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
$dimensionFilter->setDimensionName($dimension);
$dimensionFilter->setOperator("EXACT");
$dimensionFilter->setExpressions(array($dimensionFilterExpression));
//print_r($dimensionFilter);die;
// Create Segment Filter Clause.
$segmentFilterClause = new Google_Service_AnalyticsReporting_SegmentFilterClause();
$segmentFilterClause->setDimensionFilter($dimensionFilter);
// Create the Or Filters for Segment.
$orFiltersForSegment = new Google_Service_AnalyticsReporting_OrFiltersForSegment();
$orFiltersForSegment->setSegmentFilterClauses(array($segmentFilterClause));
// Create the Simple Segment.
$simpleSegment = new Google_Service_AnalyticsReporting_SimpleSegment();
$simpleSegment->setOrFiltersForSegment(array($orFiltersForSegment));
// Create the Segment Filters.
$segmentFilter = new Google_Service_AnalyticsReporting_SegmentFilter();
$segmentFilter->setSimpleSegment($simpleSegment);
// Create the Segment Definition.
$segmentDefinition = new Google_Service_AnalyticsReporting_SegmentDefinition();
$segmentDefinition->setSegmentFilters(array($segmentFilter));
// Create the Dynamic Segment.
$dynamicSegment = new Google_Service_AnalyticsReporting_DynamicSegment();
$dynamicSegment->setSessionSegment($segmentDefinition);
$dynamicSegment->setName($segmentName);
// Create the Segments object.
$segment = new Google_Service_AnalyticsReporting_Segment();
$segment->setDynamicSegment($dynamicSegment);
return $segment;
}
function getReport($analyticsreporting) {
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
if (isset($_GET['startdat']) && isset($_GET['enddate'])) {
$dateRange->setStartDate($_GET['startdat']);
$dateRange->setEndDate($_GET['enddate']);
} else {
$dateRange->setStartDate(date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 month')));
$dateRange->setEndDate(date('Y-m-d'));
}
$domain = $_GET['domain'];
$VIEW_ID = "xyz";
// Create the Metrics object.
$totalevent = new Google_Service_AnalyticsReporting_Metric();
$totalevent->setExpression("ga:totalEvents");
$totalevent->setAlias("totalEvent");
$source = new Google_Service_AnalyticsReporting_Dimension();
$source->setName("ga:source");
$eventAction = new Google_Service_AnalyticsReporting_Dimension();
$eventAction->setName("ga:eventAction");
$eventLabel = new Google_Service_AnalyticsReporting_Dimension();
$eventLabel->setName("ga:eventLabel");
$eventCategory = new Google_Service_AnalyticsReporting_Dimension();
$eventCategory->setName("ga:eventCategory");
// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges(array($dateRange));
$request->setDimensions(array($source, $eventAction, $eventLabel, $eventCategory, $segmentDimensions));
$request->setMetrics(array($totalevent));
$sourceSegment = buildSimpleSegment("Source", "ga:source", $domain);
$videoSegment = buildSimpleSegment("Category", "ga:eventCategory", "JW Player Video Plays");
$request->setSegments(array($sourceSegment, $videoSegment));
// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($request));
return $response = $analyticsreporting->reports->batchGet($body);
}