Dan*_*sen 3 php google-analytics google-api google-analytics-api google-api-php-client
按照有关如何插入webproperties的文档,以便我可以动态创建跟踪代码.目标是在Google Analytics中移动我们的分析,并在分析下自动在自己的Google帐户中创建客户网站.在我在网上找到的小东西之后,似乎这个功能可能被列入白名单.所以我正在提出这个问题来弄清楚天气是否如此.文档很难弄清楚,因为它没有告诉你需要哪些字段,字段意味着什么等.似乎文档已经过时的PHP库.不得不改变很多示例代码类名称和方法名称以使其"正常工作".
这是我用来测试此功能的代码片段.
<?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();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的例外是标题包含的内容.
| 归档时间: |
|
| 查看次数: |
1020 次 |
| 最近记录: |