JavaScript 中的 Google Analytics API
我正在使用 Google Analytics Javascript 库让用户查看他们所在页面的地理地图。但是,每次他们尝试这样做时,他们都必须经过身份验证过程才能将我的数据显示在我的页面上。我如何找到替代方案。我只想通过页面上的可视化图表嵌入我的 Analytics 数据,以便所有匿名查看者都可以看到它
**我们已经在 Google Analytics 中创建了一个帐户。 现在我们可以获取网站的统计信息。 打开网站 http://Example.com 单击授权 Analytics 按钮。 在打开的窗口中提供 Google 帐户的凭据。 用户名: [email protected] 密码:xxx 它将重定向到 http://Example.com 页面。 输入表格 ID ga:61737784 并单击绘制图表按钮,然后您将获得该网站的统计信息。
我的要求是无需登录 Google 分析网站即可获得统计信息。 因为最终用户不知道 Google 分析的凭据。 以下是 html 和 javascript 代码。**
<script src="http://apis.google.com/js/client.js?onload=gadashInit" type="text/javascript"></script>
<script src="http://analytics-api-samples.googlecode.com/svn/trunk/src/reporting/javascript/ez-ga-dash/gadash-1.0.js" type="text/javascript"></script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>
<script type="text/javascript">
gadash.configKeys({
'apiKey': xxxxx',
'clientId':xxxxx'
});
var dataOverTime = new gadash.Chart();
var scoreCard = new gadash.Chart();
var sourceMediumTable = new gadash.Chart();
var baseConfig = {
'last-n-days': 30,
'query': {
'metrics': 'ga:visitors, ga:visits, ga:pageviews',
},
'chartOptions': {
width: 400
}
};
var dataOverTimeConfig = {
'divContainer': 'dataOverTimeConfig',
'type': 'LineChart',
'query': {
'dimensions': 'ga:date',
'sort': 'ga:date'
},
'chartOptions': {
height: 250,
legend: {position: 'bottom'},
hAxis: {title:'Date'},
curveType: 'function'
}
};
var scoreCardConfig = {
'divContainer': 'scorecard',
};
var sourceMediumTableConfig = {
'divContainer': 'sourceMediumTableConfig',
'query': {
'dimensions': 'ga:source,ga:medium',
'sort': '-ga:visitors',
'max-results': 100
}
};
function renderGraph() {
baseConfig.query.ids = document.getElementById('tableId').value;
dataOverTime.set(baseConfig).set(dataOverTimeConfig).render();
scoreCard.set(baseConfig).set(scoreCardConfig).render();
sourceMediumTable.set(baseConfig).set(sourceMediumTableConfig).render();
document.getElementById('ui').style.display = 'block';
return false;
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-33166458-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32934024-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google.com/jsapi';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32934024-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + 'analytics-api-samples.googlecode.com/svn/trunk/src/reporting/javascript/ez-ga-dash/gadash-1.0.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32934024-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://') + 'apis.google.com/js/client.js?onload=gadashInit';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<button id="Button1" style="visibility: hidden;">
Authorize Analytics</button>
<hr />
<p>
Enter your Table ID:
<input type="text" id="tableId" />
<button onclick="return renderGraph()">
Draw Charts</button></p>
<hr />
<div id="ui" style="display: none;">
<h4>
Data Over Time</h4>
<div id="dataOverTimeConfig">
</div>
<h5>
Totals for each metric</h5>
<div id="scorecard">
</div>
<h5>
Source and Mediums by Visitor</h5>
<div id="sourceMediumTableConfig">
</div>
您可以使用 Google Analytics superProxy 与未经身份验证的用户共享数据。
您可以使用 superProxy 创建对 Analytics 的查询,然后使用 superProxy 存储的数据构建图表。我遇到了同样的问题,这是我能找到的唯一解决方案。
https://developers.google.com/analytics/solutions/google-analytics-super-proxy
您能否分享更多有关如何访问 Google Analytics 的信息?
查看 http://code.google.com/p/google-api-javascript-client/wiki/Authentication ,了解如何设置身份验证。请注意,您的凭据在 JavaScript 中可见,因此请确保您创建的用户仅具有所需的最低权限