1

我一直在这里学习 Hello Analytics 教程 - https://developers.google.com/analytics/solutions/articles/hello-analytics-api 我一直在这里使用 Ewan Hemings 示例 - http://www。 ewanheming.com/upload-cost-data-google-analytics尝试将我的外部成本数据上传到 Google 分析中。

我一直在使用 API Explorer - https://developers.google.com/apis-explorer/#s/analytics/v3/analytics.management.dailyUploads.upload和 post master 应用程序测试 POST 数据。虽然它没有返回任何错误,但数据没有出现在我的分析中。

我可以将数据打印到屏幕上,而且它的格式似乎正确,所以我有点难过。任何帮助都是学徒,即使您可以提供调试建议。

<?php
// The filename of the performance report
$reportFile = "Bing.csv";

// Hard code the source and medium
$source = "acme ads";
$medium = "cpc";

// Upload file headers
$headers = array(
"ga:source",
"ga:medium",
"ga:campaign",
"ga:adGroup",
"ga:adContent",
"ga:keyword",
"ga:impressions",
"ga:adClicks",
"ga:adCost"
);
$headerRow = implode(",", $headers);

// Create an array to store the data to upload
$uploadFiles = array();

// Open the performance report
$fp = fopen($reportFile, "r");

// Process each row in the file
while ($row = fgetcsv($fp)) {
// Attempt to create a date from the first column of the row
$date = isset($row[0]) ?
        date_create_from_format('M d, Y', $row[0]) : null;

// If the date creation was successful, this is a data row 
if ($date instanceof DateTime) {
    // Extract the columns from the row
    $campaign = $row[1];
    $adgroup = $row[2];
    $headline = $row[3];
    $keyword = $row[4];
    $impressions = $row[5];
    $clicks = $row[6];
    $cost = $row[7];

    // Don't upload rows with no impressions
    if ($impressions > 0) {
        // Format the date 
        $uploadDate = date_format($date, 'Y-m-d');

        // If there isn't a file for the upload date, then create one
        if (!isset($uploadFiles[$uploadDate])) {
            // Add the headers to the file
            $uploadFiles[$uploadDate] = "$headerRow\n";
        }

        // Add the row to the file
        $uploadRow = array(
            "\"$source\"",
            "\"$medium\"",
            "\"$campaign\"",
            "\"$adgroup\"",
            "\"$headline\"",
            "\"$keyword\"",
            $impressions,
            $clicks,
            $cost
        );
        $uploadFiles[$uploadDate] .= implode(",", $uploadRow) . "\n";
    }
}
}

fclose($fp);


require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_AnalyticsService.php';

session_start();

$client = new Google_Client();

$client->setApplicationName('Hello Analytics API Sample');

// Visit //code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('69673577u537j57n57j7jjm0.apps.googleusercontent.com');
$client->setClientSecret('67qg6646744724724gK');
$client->setRedirectUri('http://www.mywebsite.co.uk/analytics/GAUpload.php');
$client->setDeveloperKey('AIz42172147246h46h630JuY');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

// Magic. Returns objects from the Analytics Service instead of associative arrays.
$client->setUseObjects(true);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if (!$client->getAccessToken()) {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";

    } else {
  $analytics = new Google_AnalyticsService($client);
   foreach (array_keys($uploadFiles) as $uploadDate) {
   $analytics->management_dailyUploads->upload('46856856853', 'UA-4276753-1',     'o867567568560XJY4qg', '2013-09-02', 1, 'cost', array("reset" => true, "data" =>             $uploadFiles[$uploadDate], 'mimeType' => 'application/octet-stream', 'uploadType' =>         'media'));
}
}
?>
4

0 回答 0