0
votes

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);

}

2
Please show me what you have tried. I will see if I cant help you debug it. Seams someone forgot to add samples for filtersDaImTo
in query explorer at filter section i am using ga:source==domain name;ga:eventCategory==JW Player Video PlaysManeesh Mehta
have you gotten to work this?ichimaru

2 Answers

3
votes

A bit late but still useful, I think, because I couldn't find any answer to it on SO.

To define multiple filters you need to instance the class Google_Service_AnalyticsReporting_DimensionFilter multiple times. For example:

$filter1 = new Google_Service_AnalyticsReporting_DimensionFilter();
$filter1->setDimensionName('first_dimension_name');
$filter1->setOperator('first_operator');
$filter1->setExpressions('first_expression');

$filter2 = new Google_Service_AnalyticsReporting_DimensionFilter();
$filter2->setDimensionName('second_dimension_name');
$filter2->setOperator('second_operator');
$filter2->setExpressions('second_expression');

Then define the filter clause which tie the filters defined above:

$filter_clause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$filter_clause->setOperator('your_filter_operator');
$filter_clause->setFilters(array($filter1, $filter2));

Then do the request as usual.

0
votes

The answer @Maurizio provided is great but I needed a solution for a variable number of campaigns. Client may track one or five. Here's where I landed in case it helps anyone dealing with Google's (fairly terrible API documentation).

// Create DimensionFilters for each campaign. 
// note: $campaigns is an array supplied above this code and used below
$dimension_filters = array(); // set up an empty array 
for($i = 0; $i < count($campaigns); $i++){
    ${"dimensionFilter$i"} = new Google_Service_AnalyticsReporting_DimensionFilter();
    ${"dimensionFilter$i"}->setDimensionName('ga:campaign');
    ${"dimensionFilter$i"}->setOperator('EXACT');
    ${"dimensionFilter$i"}->setExpressions(array($campaigns[$i]));
    $dimension_filters[] = ${"dimensionFilter$i"}; // push our new DimensionFilter instance onto the array 
}

// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters(array($dimension_filters)); // now supply our final array of DimensionFilter instances