簡體   English   中英

Google Analytics(分析)-獲取URL的頁面視圖信息

[英]Google Analytics - Getting page view information for a URL

(使用Reporting API V4)每當我嘗試通過Google Analytics(分析)獲取網頁瀏覽信息時,它似乎都無法正確過濾。 例如,使用setOperator("BEGINS_WITH"); 然后setExpressions("/report"); 行,我期望的是只瀏覽以mywebsitename.com/report開頭的頁面,但是它給了我各種各樣的東西,例如除了一堆之外以/ tag和/ sponsor開頭的頁面(但是我不認為全部,但實際上不確定)/ report開頭的頁面。

在處理更多特定的URL以及使用不同的運算符時,這仍然是一個問題。 它總是返回我要尋找的內容,但也返回一堆其他似乎無關的隨機垃圾。 我也嘗試只使用正則表達式,但是仍然提供了類似的東西。

我覺得我的問題可能在於無法准確了解所有各種對象的功能(特別是在php中),但是我無法找到某些答案。

我在這里能找到的最接近的答案是這個,但是我不確定我在做什么不同於這個家伙。 我的意思是一件事是,我不是本機使用JSON字符串,但是在Google文檔中,他們使用了這種奇怪的函數語法Im正在使用。 除此之外,我認為應該大致相同。

這主要是此處此處的示例代碼的組合。

<?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");
        }
      }
    }
  }
}

這應該只是返回特定頁面路徑的視圖計數。

您可以只使用Google_Service_AnalyticsReporting_DimensionFilterGoogle_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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM