I have following code and it works just fine:
<script>
(function(w,d,s,g,js,fjs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}};
js=d.createElement(s);fjs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')};
}(window,document,'script'));
</script>
<script>
gapi.analytics.ready(function() {
var CLIENT_ID = 'my client id goes here';
gapi.analytics.auth.authorize({
container: 'auth-button',
clientid: CLIENT_ID,
});
/**
* Creates a new DataChart instance showing sessions over the past 30 days.
* It will be rendered inside an element with the id "chart-1-container".
*/
var dataChart1 = new gapi.analytics.googleCharts.DataChart({
query: {
'ids': 'ga:105112893', // The Demos & Tools website view.
'start-date': '30daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:sessions,ga:users',
'dimensions': 'ga:date'
},
chart: {
'container': 'chart-1-container',
'type': 'LINE',
'options': {
'width': '100%'
}
}
});
dataChart1.execute();
</script>
It works only if I'm logged in into google. But what I want is this to be working even if I'm not logged in. How to implement that? Could you please provide a step by step approach on how to handle that?
you can solve this by adding an acces_token to gapi.analytics.auth.authorize() we can get access_token by creating a service account in google analytics. https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/
by running a bellow java code you will get the access token
after creating service account you will get a service account mail id, you have to add this mail id in User Managment ,and you have to enable google analytics api in your account.
The Google Analytics Demos & Tools site provides a step-by-step approach on how to do that in the Embed API's server-side authorization demo:
https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/