Google Analytics每日上传使用PHP

时间:2013-01-02 12:47:20

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

我必须制作一个PHP脚本,它会使用API​​自动将数据上传到Google Analytics。但是有一个问题:developer guide仅针对python和java编写。适用于PHP的Google API客户端库已有upload method。请帮我翻译成PHP这个java代码:

  File file = new File("data.csv");
  InputStreamContent mediaContent = new InputStreamContent('application/octet-stream', new FileInputStream(file));
  mediaContent.setLength(file.length());
  Upload upload = analytics.management().dailyUploads().upload("1234",
      "UA-1234-1", "123456789", "2012-10-31", 1, "cost", mediaContent);

  upload.setReset(true);
  DailyUploadAppend append = upload.execute();

或此python代码:

media = MediaFileUpload('data.csv', mimetype='application/octet-stream', resumable=False)

daily_upload = analytics.management().dailyUploads().upload(
      accountId='1234',
      webPropertyId='UA-1234-1',
      customDataSourceId='123456789',
      date='2012-10-31',
      appendNumber=1,
      reset=true,
      type='cost',
      media_body=media).execute()

1 个答案:

答案 0 :(得分:2)

你有这个工作了吗?你有什么尝试?我不是专家,但我会抓紧抓住它。

他们网站上的PHP库肯定支持这一点,Google APIs Client Library for PHP中列出了“dailyUploads”的方法。

$dailyUploadsFile = $service->management_dailyUploads->upload(
   '12345678',                    // your accountID
   'UA-12345678-1',               // your web property ID
   'xXxXxXxXxXxXxXxXxXxXxX',      // your custom data source UID
   '2013-02-04',                  // date
   1,                             // append number
   'cost',                        // type of data
   array(
     "reset" => true,
     "data" => file_get_contents("your-data.csv"),
     'mimeType' => 'application/octet-stream',
     'uploadType' => 'media'));

这一切都来自“Google_ManagementDailyUploadsServiceResource”类。

需要对连接进行身份验证。如果您的输入文件包含多天的数据,则可以包含结束日期。