google analytics api query a specific url

99封情书 提交于 2019-11-30 05:14:28
Barmar

Use the filters option.

$OBJresult = $analytics->data_ga->get(
    'ga:' . $profilID,
    '2012-01-01',
    date("Y-m-d"),
    'ga:visits',
    array(
        'filters' => 'ga:pagePath==/news',
        'dimensions' => 'ga:pagePath',
        'metrics' => 'ga:pageviews',
        'sort' => '-ga:pageviews',
        'max-results' => '25'
    )
);

See here for the list of page tracking dimensions you can filter on.

You need to use the filters string to say "if path includes /news" which can be done as follows:

$OBJresult=$analytics->data_ga->get(
    'ga:'.$profilID,
    '2012-01-01',
    date("Y-m-d"),
    'ga:visits',
    array(
        'filters' => 'ga:pagePath=@/news',
        'dimensions' => 'ga:pagePath',
        'metrics' => 'ga:pageviews',
        'sort' => '-ga:pageviews',
        'max-results' => '25'));

The answer supplied by Barmar will only find an exact match for the /news page.

Reporting V4 example that may be useful. Thank god for these queries, their objects and poor documentation can cause severe ass cancer..

function segmentRequest(&$analyticsreporting) {

$query = [
        "viewId" => "XXXXXXX",
        "dateRanges" => [
            "startDate" => "2018-02-01",
            "endDate" => "2018-02-15"
        ],
        "metrics" => [
            "expression" => "ga:pageviews"
        ],
        "dimensions" => [
            "name" => "ga:pagepath"
        ],
        "dimensionFilterClauses" => [
            'filters' => [
                "dimension_name" => "ga:pagepath",
                "operator" => "EXACT",
                "expressions" => "/en/some_cool_page.php"
            ]
        ]
    ];

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

  printResults($response->getReports());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!