如何使用Google AnalyticsAPI为php中的频道(在“采集”下)提取信息?

时间:2016-07-18 10:54:29

标签: php facebook google-analytics google-analytics-api

我正在尝试使用Google AnalyticsAPI(PHP)将有关频道(predomidantley Facebook)的信息提取到我的系统中。我想提取的一些信息是来自每个频道的会话数量,最高登陆页面,跳出率等等,但是尽管在搜索资源管理器上尝试ga:acquisitionTrafficChannel和ga:channelGrouping似乎没有任何效果。

我已经在其他地方查看了Google API文档并浏览了各种网站,但是我被告知当前目前还没有API,或者使用上面似乎不起作用的API。

上面的两个维度/指标是否有效并且执行我想要做的事情,如果是,那么目前没有可用的API可以让我执行我需要做的事情?

谢谢,

1 个答案:

答案 0 :(得分:0)

维度ga:acquisitionTrafficChannelV4 Cohort & lifetime Value dimension,仅适用于Analytics Reporting API V4 API。查询资源管理器为built Core Reporting API V3

下面我在JSON和PHP中提供相同的示例请求。

JSON群组请求

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
  "reportRequests": 
  [
    {
      "viewId": "XXXX",
      "dimensions": 
      [
        {
          "name": "ga:cohort"
        },
        {
          "name": "ga:acquisitionTrafficChannel"
        }
      ],
      "metrics": 
      [
        {
          "expression": "ga:cohortActiveUsers"
        }
      ],
      "cohortGroup": 
      {
        "cohorts": 
        [
          {
            "name": "cohort 1",
            "type": "FIRST_VISIT_DATE",
            "dateRange": 
            {
              "startDate": "2015-08-01",
              "endDate": "2015-09-01"
            }
          },
          {
            "name": "cohort 2",
            "type": "FIRST_VISIT_DATE",
            "dateRange": 
            {
              "startDate": "2015-07-01",
              "endDate": "2015-08-01"
            }
          }
        ]
      }
    }
  ]
}

PHP群组请求

function cohortRequest(&$analyticsreporting) {
    // Create the ReportRequest object.
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId("XXXX");

    $cohortDimension = new Google_Service_AnalyticsReporting_Dimension();
    $cohortDimension->setName("ga:cohort");

    $acquisitionTrafficChannel = new Google_Service_AnalyticsReporting_Dimension();
    $acquisitionTrafficChannel->setName("ga:acquisitionTrafficChannel");

    // Set the cohort dimensions
    $request->setDimensions(array($cohortDimension, $acquisitionTrafficChannel));

    $cohortActiveUsers = new Google_Service_AnalyticsReporting_Metric();
    $cohortActiveUsers->setExpression("ga:cohortActiveUsers");

    // Set the cohort metrics
    $request->setMetrics(array($cohortActiveUsers));

    $dateRange1 = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange1->setStartDate("2015-08-01");
    $dateRange1->setEndDate("2015-09-01");

    // Create the first cohort
    $cohort1 = new Google_Service_AnalyticsReporting_Cohort();
    $cohort1->setName("cohort_1");
    $cohort1->setType("FIRST_VISIT_DATE");
    $cohort1->setDateRange($dateRange1);

    $dateRange2 = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange2->setStartDate("2015-07-01");
    $dateRange2->setEndDate("2015-08-01");

    // Create the second cohort which only differs from the first one by the date range
    $cohort2 = new Google_Service_AnalyticsReporting_Cohort();
    $cohort2->setName("cohort_2");
    $cohort2->setType("FIRST_VISIT_DATE");
    $cohort2->setDateRange($dateRange2);

    // Create the cohort group
    $cohortGroup = new Google_Service_AnalyticsReporting_CohortGroup();
    $cohortGroup->setCohorts(array($cohort1, $cohort2));

    $request->setCohortGroup($cohortGroup);

    // 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 = $analyticsreporting->reports->batchGet( $body );

    printResults($response->getReports());
}

查询资源管理器

正如评论中指出的ga:channelGrouping是一个标准的V3 API维度,它应该在Query Explorer中正常工作。正如您所见,Query Explorer is designed to only list V3 Dimensions and Metrics