gpt4 book ai didi

google-analytics - Google Analytics - 获取 URL 的页面查看信息

转载 作者:行者123 更新时间:2023-12-03 16:29:06 26 4
gpt4 key购买 nike

(使用 Reporting API V4)每当我试图通过谷歌分析获取页面查看信息时,它似乎都没有正确过滤。例如 setOperator("BEGINS_WITH");然后 setExpressions("/report");行,我希望只检索以 mywebsitename.com/report 开头的页面,但是它给了我各种各样的东西,例如以/tag 和/sponsor 开头的页面以及一堆(但我不认为所有,实际上不确定)以/report 开头的页面。

在处理更具体的 URL 以及使用不同的运算符时,这仍然是一个问题。它总是返回我正在寻找的东西,但也返回一堆似乎无关的其他随机垃圾。我也尝试只使用正则表达式,但这仍然给出了类似的东西。

我觉得我的问题可能在于不完全了解所有各种对象的作用(特别是在 php 中),但我一直无法找到其中一些的答案。

我在这里能找到的最接近的答案是 this但是我不确定我在做什么与这个人不同。我的意思是我并没有使用原生的 JSON 字符串,但是在 documentation from google他们使用我正在使用的这种奇怪的函数语法。除此之外,我认为应该几乎相同。

这主要是来自 here 的示例代码的组合和 here .

<?php

// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';


$analytics = initializeAnalytics();
//print_r($analytics);
$response = getReport($analytics);
//print_r($response);
printResults($response);


/**
* Initializes an Analytics Reporting API V4 service object.
*
* @return An authorized Analytics Reporting API V4 service object.
*/
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__ . '/service-account-credentials.json';

// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_AnalyticsReporting($client);

return $analytics;
}


/**
* Queries the Analytics Reporting API V4.
*
* @param service An authorized Analytics Reporting API V4 service object.
* @return The Analytics Reporting API V4 response.
*/
function getReport($analytics) {

// Replace with your view ID, for example XXXX.
$VIEW_ID = "HIDDEN";

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("9daysAgo");
$dateRange->setEndDate("today");

// Create the Metrics object.
$metricss = new Google_Service_AnalyticsReporting_Metric();
$metricss->setExpression("ga:pageviews");
$metricss->setAlias("views");

//Create the dimensions dimension.
$dimensions = new Google_Service_AnalyticsReporting_Dimension();
$dimensions->setName("ga:pagePath");

// 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("ga:pagePath");
$dimensionFilter->setOperator("BEGINS_WITH");
$dimensionFilter->setExpressions("/report");

// 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("Sessions with path");

// Create the Segments object.
$segment = new Google_Service_AnalyticsReporting_Segment();
$segment->setDynamicSegment($dynamicSegment);

$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges(array($dateRange));
$request->setDimensions(array($dimensions, $segmentDimensions));
$request->setSegments(array($segment));
$request->setMetrics(array($metricss));

// Create the GetReportsRequest object.
$getReport = new Google_Service_AnalyticsReporting_GetReportsRequest();
$getReport->setReportRequests(array($request));

// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$response = $analytics->reports->batchGet( $body );

return $response;
}


/**
* Parses and prints the Analytics Reporting API V4 response.
*
* @param An Analytics Reporting API V4 response.
*/
function printResults($reports) {
// print_r("reports/n");
//print_r($reports);
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++) {
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];
print($entry->getName() . ": " . $values[$k] . "\n");
}
}
}
}
}

这应该只是返回特定页面路径的 View 计数。

最佳答案

您可以通过使用 Google_Service_AnalyticsReporting_DimensionFilter 和 Google_Service_AnalyticsReporting_DimensionFilterClause 避免示例中的详细代码

所以一个例子是这样的

...

//Create the Dimensions object.
$dimension = new Google_Service_AnalyticsReporting_Dimension();
$dimension->setName("ga:pagePath");

// Create the DimensionFilter.
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName('ga:pagePath');
$dimensionFilter->setOperator('BEGINS_WITH');
$dimensionFilter->setExpressions(array('/report'));

// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters(array($dimensionFilter));

...

$request->setDimensions(array($dimension));
$request->setDimensionFilterClauses(array($dimensionFilterClause));

关于google-analytics - Google Analytics - 获取 URL 的页面查看信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56961343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com