0

Hi how can i get the metrics by device type like this list? i'm using Javascript api enter image description here

I searched the query in https://developers.google.com/analytics/devguides/reporting/core/dimsmets#q=de&mode=api&cats=user,session,traffic_sources,adwords,goal_conversions,platform_or_device,geo_network,system,social_activities,page_tracking,content_grouping,internal_search,site_speed,app_tracking,event_tracking,ecommerce,social_interactions,user_timings,exceptions,content_experiments,custom_variables_or_columns,time,doubleclick_campaign_manager,audience,adsense,channel_grouping,related_products

but i have not found also look at there https://ga-dev-tools.appspot.com/query-explorer/

this is my simple query:

    gapi.client.analytics.data.ga.get({
        'ids': 'ga:' + profileId,
        'start-date': '2015-09-06',
        'end-date': '2015-09-08',
        //'metrics': 'ga:sessions'
        'metrics':'ga:visits,ga:sessions,ga:pageviews,ga:browser'
      }).

then(function(response) {
grijalvaromero
  • 589
  • 1
  • 6
  • 20

2 Answers2

0

You have a wrong parameter in the "metrics" part of your query (ga:browser is not a metric) and you don't query any dimension. While querying only metrics is technically valid the thing you are looking after is a dimension, specifically ga:deviceCategory. This will return your dimensions broken down by device category which has three possible values, desktop, mobile and tablet. So this should probably look something like:

 gapi.client.analytics.data.ga.get({
        'ids': 'ga:' + profileId,
        'start-date': '2015-09-06',
        'end-date': '2015-09-08',
        'metrics':'ga:visits,ga:sessions,ga:pageviews',
        'dimensions':'ga:deviceCategory'
      }).
Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
0

Thanks finally i found the solution this is my code:

gapi.client.analytics.data.ga.get({
    'ids': 'ga:' + profileId,
    'start-date': '31daysAgo',
    'end-date': 'today',
    'dimensions': 'ga:pageTitle,ga:deviceCategory', 
    'metrics':'ga:visits,ga:sessions,ga:pageviews'
  }).then(function(response) {
    var formattedJson = JSON.stringify(response.result, null, 2);
    //send the values to some label
    $("#visitasReport").text(response.result.totalsForAllResults["ga:visits"]);
    $("#duracionReport").text(response.result.totalsForAllResults["ga:sessions"]);

    var arregloPaginas = [],arregloDevices = [];
    var totalTablet=0,totalMobil=0,totalDesktop=0;
    function Array(t, a, l) {
        this.titulo = t;
        this.a = a;
    }

    for(var x=0;x<response.result.rows.length;x++){
        if(response.result.rows[x][2]!=0){
            var a = new Array(response.result.rows[x][0],response.result.rows[x][2]);
            arregloPaginas.push(a);
            //calculate the numbers of visits view the  console.log(response.result)
            if(response.result.rows[x][1]=="mobile"){totalMobil+=parseInt(response.result.rows[x][2]);}
            if(response.result.rows[x][1]=="desktop"){totalDesktop+=parseInt(response.result.rows[x][2]);}
            if(response.result.rows[x][1]=="tablet"){totalTablet+=parseInt(response.result.rows[x][2]);}
        }

    }
    $("#reportDesktop").text(totalDesktop);
    $("#reportMobile").text((totalTablet+totalMobil));
    var totalVisits=response.result.totalsForAllResults["ga:visits"];
    //send data to Charts
    Morris.Bar({
            element: 'morris-bar-chart',
            data:arregloPaginas,
            xkey: 'titulo',
            ykeys: ['a'],
            labels: ['Visitas'],
            hideHover: 'auto',
            resize: true
        });
    Morris.Donut({
        element: 'morris-donut-chart',
        data: [
            {label: "Celulares",value: Math.round((totalMobil*100)/totalVisits)}, 
            {label: "Escritorio",value: Math.round((totalDesktop*100)/totalVisits)},
            {label: "Tablets",value: Math.round((totalTablet*100)/totalVisits)}
        ],
        resize: true
    });

    /********************************/
  }).then(null, function(err) {
          // Log any errors.
          console.log(err);
  });

This is the dashboard

Dashboard

grijalvaromero
  • 589
  • 1
  • 6
  • 20
  • 1
    You can accept your own answer - won't get you any points, but the question will disappear from the "needs answer" tab, so other people become aware that this is already solved. – Eike Pierstorff Sep 10 '15 at 07:29