I am trying to build a multiple line chart with Google Analytics Reporting API v4.
A chart where I have a line for each device (Desktop/Tablet/Mobile) by session count by day.
But for now all I can get is this:
And my code is:
<div id="chart-1-container"></div>
<script>
gapi.analytics.ready(function () {
var dataChart1 = new gapi.analytics.googleCharts.DataChart({
query: {
'ids': 'ga:XX', // <-- Replace with the ids value for your view.
'start-date': '7daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:sessions',
'dimensions': 'ga:deviceCategory'
},
chart: {
'container': 'chart-1-container',
'type': 'LINE',
'options': {
'width': '100%'
}
}
});
dataChart1.execute();
});
</script>
Based on the answer of this question - Google Analytics API deviceCategory - I finally found the problem.
To get a specific chart based on a category like mobile, the data is build based on filters and not on dimensions like I was trying to achieve:
<div id="chart-1-container"></div>
<script>
gapi.analytics.ready(function () {
var dataChart1 = new gapi.analytics.googleCharts.DataChart({
query: {
'ids': 'ga:XX', // <-- Replace with the ids value for your view.
'start-date': '7daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:sessions',
'dimensions': 'ga:date',
'filters': 'ga:deviceCategory==mobile' // <-- Filter the category here
},
chart: {
'container': 'chart-1-container',
'type': 'LINE',
'options': {
'width': '100%'
}
}
});
dataChart1.execute();
});
</script>
And that's it: