Google Analytics簡稱GA,以下簡稱GA,使用部分功能需要翻牆。了解GA,官方文檔

使用GA,我們可以得到使用者在網站上做了些什麼,什麼時候通路等,這些資訊對于網站經營者的導向有很重要的價值。上圖是google分析中的所有的API,看着很多,各種API,但是入門隻需要掌握
analytics.js
和
Core Reporting API
就可以。
使用步驟:
- 1.建立一個GA分析項目
- 2.給網站加入跟蹤代碼,使用
analytics.js
跟蹤頁面,最簡單的DEMO在第一步的項目中已給出。參考文檔 。
- 3.如果用GA自帶的可視化界面(在第一部的項目中就可以看到),那麼就到此結束,如果你需要自己顯示資料,那麼就需要使用
Core Reporting API
。參考文檔在此,在這個子產品我使用的是JavaScript實作的。
首先分析第一個步驟,建立一個GA分析項目,我們需要搞懂一些概念,賬戶結構,google賬戶->google分析賬戶->媒體資源(property)->資料視圖(profile),層層遞進的關系,上級可以建立多個下級,我們隻有一步步注冊到資料視圖之後,我們才可以獲得并使用跟蹤代碼,跟蹤代碼可以跟蹤很多東西,有高深的寫法,這裡自行研究。
第二個步驟,我們可以在建立的資料視圖中得到跟蹤代碼,直接加入頁面就可以擷取資料,在這裡我們怎麼知道是否成功了呢,可以等幾小時,也可以找到實時(real time)項,看此時有幾個使用者通路,你通路一次,如果數字有變化,那麼說明成功。注意這裡的跟蹤代碼的使用不需要翻牆,但是看幫助文檔需要翻牆的。
第三個步驟(需要翻牆),我隻講使用core Reporting API(有兩個版本v3和v4,我使用的是v3),我使用的是 JavaScript
,是以我隻講js,要使用js開發,需要在 google開發者api控制台,建立一個項目,在API管理器的庫欄開啟 Google Analytics Reporting API
(可能還需要開啟 Analytics API
),然後在憑據欄建立憑據,建立一個OAuth用戶端ID,應用類型選擇網頁應用,限制第一個空輸入 http://www.domin.cn
,第二個空輸入 http://www.domin.cn/oauth2callback
,domin為你的域名,這裡靈活應用,不太懂機理(一開始按照文檔中設定總是出問題 ,後來直接使用了自己的域名才成功),擷取了用戶端 ID,然後運作了示例代碼,獲得授權登入Google賬戶,即可顯示擷取内容(翻牆)。官方有一個擷取資料DEMO,可以研究擷取資料的寫法。分析代碼:
JavaScript
Google Analytics Reporting API
Analytics API
http://www.domin.cn
http://www.domin.cn/oauth2callback
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Analytics - A quickstart guide for JavaScript</title>
</head>
<body>
<button id="auth-button" hidden>Authorize</button>
<h1>Hello Analytics</h1>
<textarea cols="80" rows="20" id="query-output"></textarea>
<script>
// Replace with your client ID from the developer console.
var CLIENT_ID = '<YOUR_CLIENT_ID>';//此處填寫你的用戶端ID
// Set authorized scope.
var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];
function authorize(event) {
// Handles the authorization flow.
// `immediate` should be false when invoked from the button click.
var useImmdiate = event ? false : true;
var authData = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: useImmdiate
};
gapi.auth.authorize(authData, function(response) {
var authButton = document.getElementById('auth-button');
if (response.error) {
authButton.hidden = false;
}
else {
authButton.hidden = true;
queryAccounts();
}
});
}
function queryAccounts() {
// Load the Google Analytics client library.
gapi.client.load('analytics', 'v3').then(function() {//加載API的庫
// Get a list of all Google Analytics accounts for this user
gapi.client.analytics.management.accounts.list().then(handleAccounts);
});
}
function handleAccounts(response) {
// Handles the response from the accounts list method.
if (response.result.items && response.result.items.length) {
// Get the first Google Analytics account.
var firstAccountId = response.result.items[].id;//擷取第一個google分析賬戶(一個google賬戶可以有很多個google分析賬戶)
// Query for properties.
queryProperties(firstAccountId);
} else {
console.log('No accounts found for this user.');
}
}
function queryProperties(accountId) {
// Get a list of all the properties for the account.
gapi.client.analytics.management.webproperties.list(
{'accountId': accountId})
.then(handleProperties)
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}
function handleProperties(response) {
// Handles the response from the webproperties list method.
if (response.result.items && response.result.items.length) {
// Get the first Google Analytics account
var firstAccountId = response.result.items[].accountId;//第0個google分析賬戶ID
// Get the first property ID
var firstPropertyId = response.result.items[].id;//擷取第0個媒體資源(property)ID
// Query for Views (Profiles).
queryProfiles(firstAccountId, firstPropertyId);
} else {
console.log('No properties found for this user.');
}
}
function queryProfiles(accountId, propertyId) {//參數有兩個,google分析賬戶ID,媒體資源ID
// Get a list of all Views (Profiles) for the first property
// of the first Account.
gapi.client.analytics.management.profiles.list({
'accountId': accountId,
'webPropertyId': propertyId
})
.then(handleProfiles)
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}
function handleProfiles(response) {
// Handles the response from the profiles list method.
if (response.result.items && response.result.items.length) {
// Get the first View (Profile) ID.
var firstProfileId = response.result.items[].id;//第0個資料視圖ID
// Query the Core Reporting API.
queryCoreReportingApi(firstProfileId);
} else {
console.log('No views (profiles) found for this user.');
}
}
function queryCoreReportingApi(profileId) {//根據資料視圖ID獲得資料,一切建立在授權成功的基礎上
// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'today',
'metrics': 'ga:sessions'
})
.then(function(response) {
var formattedJson = JSON.stringify(response.result, null, );
document.getElementById('query-output').value = formattedJson;
})
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}
// Add an event listener to the 'auth-button'.
document.getElementById('auth-button').addEventListener('click', authorize);
</script>
<script src="https://apis.google.com/js/client.js?onload=authorize"></script>
</body>
</html>