google analytics api查询特定网址

时间:2022-02-08 14:29:40

I am accessing the Google analytics API with PHP which works on my end but I'd love to filter the results a bit further. Right now I am using:

我正在使用PHP访问Google分析API,这可以在我的最后工作,但我希望能够进一步过滤结果。现在我正在使用:

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

Currently this returns a set of 25 pages sorted by its hits. I would love to restrict the results to a specific path within the server. So e.g. only query domain.com/news and only see what the most hit news pages are. I can filter with PHP but rather have the query as specific as possible.

目前,这将返回按其命中排序的25页。我希望将结果限制在服务器内的特定路径。所以例如只查询domain.com/news,只查看最受欢迎的新闻页面。我可以使用PHP过滤,而是尽可能具体地查询。

Thanks for the help

谢谢您的帮助

3 个解决方案

#1


19  

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.

请点击此处查看您可以过滤的网页跟踪尺寸列表。

#2


11  

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

您需要使用filters字符串来说“if path includes / news”,可以按如下方式完成:

$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.

Barmar提供的答案只会找到/ news页面的完全匹配。

#3


0  

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

报告可能有用的V4示例。感谢上帝对这些疑问,他们的对象和糟糕的文档可能导致严重的屁股癌症..

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());
}

#1


19  

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.

请点击此处查看您可以过滤的网页跟踪尺寸列表。

#2


11  

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

您需要使用filters字符串来说“if path includes / news”,可以按如下方式完成:

$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.

Barmar提供的答案只会找到/ news页面的完全匹配。

#3


0  

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

报告可能有用的V4示例。感谢上帝对这些疑问,他们的对象和糟糕的文档可能导致严重的屁股癌症..

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());
}