[英]Google Analytics API Embed Not Populating Data
I am working with the Google Analytics API for Javascript embeds. 我正在使用适用于Javascript嵌入的Google AnalyticsAPI。 I am working on a local server (XAMPP).
我正在使用本地服务器(XAMPP)。 I can authenticate, or login to the service.
我可以进行身份验证或登录该服务。 However, none of the div content (or actual data) populates.
但是,没有任何div内容(或实际数据)填充。 I'm not quite sure where I am going wrong.
我不太确定我哪里出错了。 I will attach the code below.
我将附上以下代码。 Please note that the "//My-GA-ID//" has been replaced for the supplied code.
请注意,“// My-GA-ID //”已替换为提供的代码。 I do have a functional ID.
我有一个功能ID。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
(function(w, d, s, g, js, fs){
g = w.gapi || (w.gapi = {});
g.analytics = {
q: [],
ready: function(f){
this.q.push(f);
}
};
js = d.createElement(s);
fs = d.getElementsByTagName(s)[0];
js.src = 'https://apis.google.com/js/platform.js';
fs.parentNode.insertBefore(js, fs);
js.onload = function(){
g.load('analytics');
};
}(window, document, 'script'));
</script>
<script type="text/javascript">
gapi.analytics.ready(function(){
/**
* Authorize the user immediately if the user has already granted access.
* If no access has been created, render an authorize button inside the
* element with the ID "embed-api-auth-container".
*/
gapi.analytics.auth.authorize({
container: 'embed-api-auth-container',
clientid: '//MY-GA-ID//'
});
/**
* Create a new ViewSelector instance to be rendered inside of an
* element with the id "view-selector-container".
*/
var viewSelector = new gapi.analytics.ViewSelector({
container: 'view-selector-container'
});
// Render the view selector to the page.
viewSelector.execute();
/**
* Create a new DataChart instance with the given query parameters
* and Google chart options. It will be rendered inside an element
* with the id "chart-container".
*/
var dataChart = new gapi.analytics.googleCharts.DataChart({
query: {
metrics: 'ga:sessions',
dimensions: 'ga:date',
'start-date': '30daysAgo',
'end-date': 'yesterday'
},
chart: {
container: 'chart-container',
type: 'LINE',
options: {
width: '100%'
}
}
});
/**
* Render the dataChart on the page whenever a new view is selected.
*/
viewSelector.on('change', function(ids){
dataChart.set({
query: {
ids: ids
}
}).execute();
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
</head>
<body>
<h1>New Web Project Page</h1>
Here is the auth:
<div id="embed-api-auth-container">
</div>
<br>
Here is the chart
<div id="chart-container">
</div>
<br>
Here is the View
<div id="view-selector-container" style="width: 400px; height: 300;">
</div>
</body>
</html>
Take a look at the Example in the docs . 看一下docs中的Example 。 You need to wait to execute the
viewSelector
for after you authorize the application. 在授权应用程序之后,您需要等待执行
viewSelector
。 Replace the line viewSelector.execute();
替换行
viewSelector.execute();
with: 有:
gapi.analytics.auth.on('success', function(response) {
viewSelector.execute();
});
And things should work out a little bit better. 事情应该好一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.