标签添加全局代码:
代码位置:“媒体资源”栏⽬下“.js 跟踪代码”->“跟踪代码”
第 1 步:启⽤ Google Analytics(分析)API工作检讨书
要开始使⽤ Google Analytics(分析)API,需要先使⽤设置⼯具,该⼯具会引导您在 Google API 控制台中创建项⽬,启⽤ API 以及创建凭据。
创建客户端 ID
打开服务帐号页。如果看到相关提⽰,请选择项⽬。
六一儿童节的句子点击创建服务帐号。
在创建服务帐号窗⼝中,键⼊服务帐号的名称,然后选择提供新的私钥。如果您希望将 G Suite 全⽹域权限授予该服务帐号,另请选中启⽤ G Suite 全⽹域委派功能。然后点击保存。
您的新公钥/私钥对已⽣成并下载到您的计算机;该密钥仅此⼀份,您负责安全存储该密钥。
当系统提⽰您提供“密钥类型”时,选择 JSON,并将⽣成的密钥另存为 service-account-credentials.json;
在本教程的后⾯,您将需要⽤到该密钥。
将服务帐号添加到 Google Analytics(分析)帐号中
新建服务帐户的电⼦邮件地址为 <projectId>-<uniqueId>@developer.gserviceaccount,可⽤于向您想通过该 API 访问的 Google Analytics(分析)帐户添加⽤户。就本教程涵盖的内容⽽⾔,只需阅读和分析权限即可。
第 2 步:安装 Google 客户端库
要获得适⽤于 PHP 的 Google API 客户端库,您可以下载此版本或使⽤ Composer:
composer require google/apiclient:^2.0
第 3 步:设置⽰例代码
您需要创建⼀个名为 HelloAnalytics.php 的⽂件,其中将包含以下⽰例代码。
将以下源代码复制或下载到 HelloAnalytics.php 中。
将先前下载的 service-account-credentials.json 移到⽰例代码所在的⽬录中。
⾸先要先在后套
<?php
use Hisune\EchartsPHP\ECharts;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use \kartik\date\DatePicker;
$this->title = '管理后台';
?>
<div >
<?php $form = ActiveForm::begin(['action' => ['index/index'], 'method' => 'post']); ?>
<div>
<div >
<?php
echo DatePicker::widget([公司授权委托书范本
'model' => $model,
'attribute' => 'start_date',
'attribute2' => 'end_date',
'options' => ['placeholder' => '开始时间'],
'options2' => ['placeholder' => '结束时间'],
'type' => DatePicker::TYPE_RANGE,
'separator' => '-',
'pluginOptions' => [
'endDate'=>'+1',
'format' => 'yyyy-mm-dd',
'autoclose' => true,
]
]);
?>
</div>
<div class="form-group" >
<?= Html::submitButton('搜索', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
$chart = new ECharts();
// $day = date('Y-m-d', time()); //获取当天⽇期
$chart->title->text = '浏览量(总浏览量'.$visite.')'; //标题
// $chart->title->subtext = '⽇期 ' . $day; //副标题
$chart->title->left = 'center'; //标题距离左侧的距离,这⾥设为居中
$chart->tooltip->show = true; //提⽰框显⽰
$chart->tooltip->trigger = 'axis'; //数据项图形触发
$chart->legend->data = ['浏览量', '访客数', '新访客数']; //图例组件 ,'IP数'
$chart->legend->left = 'right'; //图例组件显⽰在右边
$chart->color = ['#d14a61', '#3c8dbc', 'green', 'orange']; //颜⾊就在这⾥定义,series会按顺序使⽤这些颜⾊。$chart->xAxis = array(
'type' => 'category',
'data' => $data['DAY'],
);
$chart->yAxis = [
[
'type' => 'value',
'name' => '浏览量',
热爱祖国的诗句'min' => 0,
'axisLine' => [花好月圆歌词
'lineStyle' => [
'color' => 'green'//定义Y轴颜⾊
]
],
'axisLabel' => [
'formatter' => "{value}"//定义Y轴刻度标签
],
],
];
$chart->series = [
[
'name' => '浏览量',
'type' => 'line',
'data' => $data['DAV'],
],
// [
// 'name' => 'IP数',
// 'type' => 'line',
// 'data' => $data['DIP'],
// ],
[
'name' => '访客数',
'type' => 'line',
'data' => $data['DAU'],
],
[
'name' => '新访客数',
'type' => 'line',
'data' => $data['DNU'],
],
];
echo $chart->render('simple-custom-1');
?>
public function initializeAnalytics()
{
// Use the developers console and download your service account
// credentials in JSON format. Place them in this directory or
// change the key file location if necessary.
$KEY_FILE_LOCATION = __DIR__ . '/client_secrets.json';
// Create and configure a new client object.
$client = new \Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setDeveloperKey('OAuth 客户端的ID');
$client->setScopes(['leapis/adonly']);
$analytics = new \Google_Service_AnalyticsReporting($client);
return $analytics;
}
public function actionIndex()
{
$analytics = $this->initializeAnalytics();
$data = $this->getReport($analytics);
return $this->render('index', $data);
}
/**
* Queries the Analytics Reporting API V4.
*
* @param service An authorized Analytics Reporting API V4 service object.
* @return The Analytics Reporting API V4 response.
*/
public function getReport($analytics) {
$model = new \app\models\DayCount();
$startDate = date('Y-m-d', strtotime('-7 day'));
$endDate = date('Y-m-d', time());
if (Yii::$app->request->isPost) {
$model->load(Yii::$app->request->post());
$startDate = date('Y-m-d',strtotime($model->start_date));
$endDate = date('Y-m-d',strtotime($model->end_date)) ;
}
else{
$model->start_date = date('Y-m-d', strtotime($startDate));
$model->end_date = date('Y-m-d', strtotime($endDate));
}
// Replace with your view ID, for example XXXX.
$VIEW_ID = "216828575"; //数据视图ID
// 配置时间范围
$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($startDate);
$dateRange->setEndDate($endDate); //"2020-05-05"
// 配置指标(显⽰的字段). ⽅法⼀
// $sessions = new \Google_Service_AnalyticsReporting_Metric();
// $sessions->setExpression("ga:pageviews");// sessions
// $sessions->setAlias("pageviews");
// $sessions2 = new \Google_Service_AnalyticsReporting_Metric();
// $sessions2->setExpression("ga:users");// users
// $sessions->setAlias("users");
//配置维度 (每⽇分组)
$browser = new \Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:date");// ga:day ga:nthDay date
// Create the ReportRequest object.
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setDimensions($browser);
//配置查询指标(显⽰的字段). ⽅法⼆
$request->setMetrics([["expression"=>'ga:users'],["expression"=>'ga:pageviews'],["expression"=>'ga:newUsers']]);//array($sessions,$sessions2)
$request->setMetricFilterClauses([[
"filters"=>[[
"metricName"=> "ga:users",
"operator"=> "GREATER_THAN", //GREATER_THAN MORE_THAN
"comparisonValue"=> "-1"
]]
]]);
$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$data = $analytics->reports->batchGet($body );
$data_new = $this->printResults($data);
$visite = array_sum($data_new['DAV']);
return ['data'=>$data_new,'model'=>$model,'visite'=>$visite];
}
/
**
* Parses and prints the Analytics Reporting API V4 response.
*
* @param An Analytics Reporting API V4 response.
*/
function printResults($reports) {
$data = [];
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
//维度信息
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) { $data['DAY'][] = $dimensions[$i];
// print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
}
//指标信息
for ($j = 0; $j < count($metrics); $j++) {
$values = $metrics[$j]->getValues();
for ($k = 0; $k < count($values); $k++) {
$entry = $metricHeaders[$k];
//每⽇浏览量
if($entry->getName() == 'ga:pageviews'){
$data['DAV'][] = $values[$k];
}
//每⽇访客数
if($entry->getName() == 'ga:users'){
$data['DAU'][] = $values[$k];
}
//每⽇新访客数
if($entry->getName() == 'ga:newUsers'){
$data['DNU'][] = $values[$k];
}
// $data[$entry->getName()] = $values[$k];
// print($entry->getName() . ": " . $values[$k] . "\n");
}
}
}
}
重装系统win7旗舰版return $data;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论