在请求多渠道路径之前,我正在使用Core Reporting API,并且运行良好,但是现在我需要多渠道路径数据,并且已经阅读了Google提供的MCF完整指南。
但是我不确定该怎么做。下面,我粘贴我以前的核心报告api代码,该代码运行良好。现在,我应该为多渠道路径api请求做什么。
我必须使用另一个php库或另一个函数,还是应该使用与核心报告api相同的函数。
将参数传递给自定义创建的函数以调用库函数
$arrMetrics = ['ga:users' => 'Users', 'ga:adClicks' => 'Clicks', 'ga:adCost' => 'Cost', 'ga:CPC' => 'CPC'];
$duration = $_POST['chart_duration'];
$arrParam = getDurationFromText($_POST['chart_duration']);
$arrConfig = [];
$arrConfig['StartDate'] = $arrParam['StartDate'];
$arrConfig['EndDate'] = $arrParam['EndDate'];
$analyticsData = $ga->getReport($site,$arrMetrics,$arrConfig);
if($analyticsData['is_success'])
{
$arrProccessData = printResults($analyticsData['data']);
$arrData = [];
$arrData['Clicks']['data'] = $arrProccessData[0][0]['metrics']['Clicks'];
$arrData['Cost']['data'] = round($arrProccessData[0][0]['metrics']['Cost'], 2);
$arrData['CPC']['data'] = round($arrProccessData[0][0]['metrics']['CPC'], 2);
$arrTotal = [];
foreach($arrData as $key => $data)
{
$arrTotal[$key] = $data['data'];
}
$arrResult['is_success'] = true;
$arrResult['total'] = $arrTotal;
}
else
{
$arrResult['is_success'] = false;
$arrResult['message'] = $analyticsData['error']->error->message;
}
这是getReport函数
function getReport($objSite,$arrMetrics,$arrConfig) {
$this->client->setAccessToken($objSite->accessToken);
$analytics = new Google_Service_AnalyticsReporting($this->client);
// Replace with your view ID, for example XXXX.
$VIEW_ID = $objSite->project_id;
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
// Create the Metrics object.
$setMetrics=[];
foreach($arrMetrics as $key => $alias)
{
$ga_metrics = new Google_Service_AnalyticsReporting_Metric();
$ga_metrics->setExpression($key);
$ga_metrics->setAlias($alias);
$setMetrics[] = $ga_metrics;
}
$request->setMetrics($setMetrics);
//Create the Dimensions object.
if(!empty($arrConfig['dimensions']))
{
$setDimensions=[];
foreach($arrConfig['dimensions'] as $dimensions)
{
$objDimensions = new Google_Service_AnalyticsReporting_Dimension();
$objDimensions->setName($dimensions);
$setDimensions[] = $objDimensions;
}
$request->setDimensions($setDimensions);
}
if(!empty($arrConfig['filter']))
{
$setFilter=[];
foreach($arrConfig['filter'] as $filter)
{
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName($filter['dimension']);
$dimensionFilter->setOperator($filter['operator']);
$dimensionFilter->setExpressions([$filter['value']]);
$setFilter[] = $dimensionFilter;
}
// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters($setFilter);
$request->setDimensionFilterClauses(array($dimensionFilterClause));
}
if(!empty($arrConfig['StartDate']))
{
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($arrConfig['StartDate']);
$dateRange->setEndDate($arrConfig['EndDate']);
$request->setDateRanges($dateRange);
}
if(!empty($arrConfig['sort']))
{
$orderby =new Google_Service_AnalyticsReporting_OrderBy();
$orderby->setFieldName($arrConfig['sort']['field']);
$orderby->setSortOrder($arrConfig['sort']['order']);
$request->setOrderBys($orderby);
}
if(!empty($arrConfig['page_size']))
{
$request->setPageSize($arrConfig['page_size']);
}
$return = [];
try{
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$return['data'] = $analytics->reports->batchGet( $body );
$return['is_success'] = true;
}
catch(exception $e)
{
$return['is_success'] = false;
$return['error'] = json_decode($e->getMessage());
}
return $return;
}
在阅读了一些指南并提供了帮助之后,我才知道必须调用不同的类和方法,但仍然感到困惑。