php - Google Analytics Daily Upload Script not working -
i have been working way through hello analytics tutorial here - https://developers.google.com/analytics/solutions/articles/hello-analytics-api , have been using ewan hemings example here - http://www.ewanheming.com/upload-cost-data-google-analytics try , upload external cost data google analytics's.
i have been testing post data using api explorer - https://developers.google.com/apis-explorer/#s/analytics/v3/analytics.management.dailyuploads.upload , post master app. although not returning errors data not appearing in analytics.
i can print data screen , seems formatted correctly bit stumped. apprenticed, if offer suggestion debugging.
<?php // filename of performance report $reportfile = "bing.csv"; // hard code source , 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 array store data upload $uploadfiles = array(); // open performance report $fp = fopen($reportfile, "r"); // process each row in file while ($row = fgetcsv($fp)) { // attempt create date first column of row $date = isset($row[0]) ? date_create_from_format('m d, y', $row[0]) : null; // if date creation successful, data row if ($date instanceof datetime) { // extract columns 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 no impressions if ($impressions > 0) { // format date $uploaddate = date_format($date, 'y-m-d'); // if there isn't file upload date, create 1 if (!isset($uploadfiles[$uploaddate])) { // add headers file $uploadfiles[$uploaddate] = "$headerrow\n"; } // add row 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 generate // client id, client secret, , register 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 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) $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')); } } ?>
Comments
Post a Comment