Pregunta

Been following the documentation on how to insert webproperties so that i can create tracking codes dynamically. The objective is to move our analytics over in Google Analytics & automatically creating the customers website on their own google account under analytics. After what little i could find online it seems like this feature might be whitelisted. So I'm making this question to figure out weather or not this is the case. Documentation is hard to figure out, cause it doesn't tell you what fields are required, what the fields mean etc. Also seems like the documentation is outdated for the PHP library. Had to change alot of the example code class names & method names to get it "working".

Here is the code snippet that i use to test this feature out.

<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
require_once 'Google/Service/Oauth2.php';

session_start();

$client = new Google_Client();

$client->setClientId('xxxxx');
$client->setClientSecret('xxxxxx');
$client->setRedirectUri('xxxxxxxx');
$client->setDeveloperKey('xxxxxxx');
$client->setScopes(
    array(
          'https://www.googleapis.com/auth/analytics.readonly',
          'https://www.googleapis.com/auth/analytics',
          'https://www.googleapis.com/auth/userinfo.profile',
          'https://www.googleapis.com/auth/analytics.edit',
          'https://www.googleapis.com/auth/analytics.manage.users'
    )
);

$oauth2 = new Google_Service_Oauth2($client);

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

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

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

if (!$client->getAccessToken()) {
  $authUrl = $client->createAuthUrl();

  print "<a class='login' href='$authUrl'>Connect Me!</a>";

} else {
    $analytics = new Google_Service_Analytics($client);

    try {
        $user = $oauth2->userinfo->get();

        $permission = new Google_Service_Analytics_WebpropertyPermissions();
        $permission->setEffective(array('EDIT', 'VIEW'));

        $trackingObject = new Google_Service_Analytics_Webproperty();
        $trackingObject->setAccountId($user['id']);
        //$trackingObject->setDefaultProfileId($user['id']);
        $trackingObject->setId('UA-xxxxx-1');
        $trackingObject->setPermissions($permission);
        $trackingObject->setIndustryVertical('INTERNET_AND_TELECOM');
        $trackingObject->setLevel('STANDARD');
        $trackingObject->setName('xxxxxx');
        $trackingObject->setWebsiteUrl('xxxxxx');

        $analytics->management_webproperties->insert($user['id'], $trackingObject);
        $accounts = $analytics->management_accounts->listManagementAccounts();
        echo '<pre>'; print_r($user); echo '</pre>';
        echo '<pre>'; print_r($accounts); echo '</pre>';
        die();

  } catch (apiServiceException $e) {
    // Error from the API.
    print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();

  } catch (Exception $e) {
    print 'There was a general error : ' . $e->getMessage();
  }
}

The exception i get is what title contains.

¿Fue útil?

Solución

Write operations are still in beta. Before you can access this feature you need to request access to the beta.

Write operations in the Management API (e.g. create, update, delete, patch) for Web Property, View (Profile), and Goal resources is currently available as a developer preview in limited beta. If you're interested in using these features, request access to the beta.

Update: It takes around 3 weeks to get approved. When you are approved you will receive an email from Google inviting you to join the beta test group.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top