javascript-GA GEO图表城市标签错误

codeday· 2019-11-22
本文来自 codeday ,作者 codeday
我有一个页面连接到Google Analytics(分析)帐户.在页面上,我有一个专注于佛罗里达州的GEO图表.

当我将鼠标悬停在地图上的标记上时,大约一半的标记似乎是正确的.另一半标有与地图上标记不符的城市名称.就像该州中部的标记被标记为巴西的城市.有关示例,请参见下面的屏幕截图.

这是设置GEO图表的代码.知道我在做什么错吗?

这也是jsbin

(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'));




gapi.analytics.ready(function() {

  var CLIENT_ID = '663097249213-ankiu39ud1m7imaa4r5h5mtt4bnpp9b5.apps.googleusercontent.com';

  gapi.analytics.auth.authorize({
    container: 'auth-button',
    clientid: CLIENT_ID,
  });

  var viewSelector = new gapi.analytics.ViewSelector({
   container: 'view-selector'
  });

  var stateMap = new gapi.analytics.googleCharts.DataChart({
    query: {
      'dimensions': 'ga:city',
      'metrics': 'ga:sessions',
      'start-date': '14daysAgo',
      'end-date': 'yesterday',
    },
    chart: {
      type: 'GEO',
      container: 'state',
      options: {
        region: 'US-FL', 
        resolution: 'metros',
        displayMode: 'markers',
        width:'100%',
        height:'100%',
        keepAspectRatio:true
      }
    }
  });



  gapi.analytics.auth.on('success', function(response) {
    viewSelector.execute();
    $('#auth-button').hide();
  });



  viewSelector.on('change', function(ids) {
    var newIds = {
      query: {
        ids: ids
      }
    };
    stateMap.set(newIds).execute();
  });
});

以下是请求标头:

Remote Address:74.125.137.95:443
Request URL:https://content.googleapis.com/analytics/v3/data/ga?start-date=7daysAgo&end-date=yesterday&dimensions=ga%3AuserType&metrics=ga%3Asessions&sort=-ga%3Asessions&max-results=9&ids=ga%3A91097884&output=dataTable&embedApiVersion=v1
Request Method:GET
Status Code:200 OK
Request Headers
:host:content.googleapis.com
:method:GET
:path:/analytics/v3/data/ga?start-date=7daysAgo&end-date=yesterday&dimensions=ga%3AuserType&metrics=ga%3Asessions&sort=-ga%3Asessions&max-results=9&ids=ga%3A91097884&output=dataTable&embedApiVersion=v1
:scheme:https
:version:HTTP/1.1
accept:*/*
accept-encoding:gzip,deflate,sdch
accept-language:en-US,en;q=0.8
authorization:Bearer ya29.qgAhgT4Oq1u6e_cfcv1qP2ieP_QUbPM0WozLPpagwZ_9rmXM8G0s0uDK
cache-control:no-cache
pragma:no-cache
referer:https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.FnxxMUT2ulE.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCMEC-DWdYC4P42L_5byzwhSZYF3Cg
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
x-client-data:CJe2yQEIprbJAQiptskBCMG2yQEI8IjKAQiPlMoBCN6WygE=
x-clientdetails:appVersion=5.0%20(Macintosh%3B%20Intel%20Mac%20OS%20X%2010_10_0)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F38.0.2125.104%20Safari%2F537.36&platform=MacIntel&userAgent=Mozilla%2F5.0%20(Macintosh%3B%20Intel%20Mac%20OS%20X%2010_10_0)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F38.0.2125.104%20Safari%2F537.36
x-goog-encode-response-if-executable:base64
x-javascript-user-agent:google-api-javascript-client/1.1.0-beta
x-origin:http://affordable-glass.com
x-referer:http://affordable-glass.com
Query String Parametersview sourceview URL encoded
start-date:7daysAgo
end-date:yesterday
dimensions:ga:userType
metrics:ga:sessions
sort:-ga:sessions
max-results:9
ids:ga:91097884
output:dataTable
embedApiVersion:v1
Response Headers
alternate-protocol:443:quic,p=0.01
cache-control:private, max-age=0, must-revalidate, no-transform
content-encoding:gzip
content-length:555
content-type:application/json; charset=UTF-8
date:Sun, 26 Oct 2014 00:35:09 GMT
etag:"C7PuqpcNYAngGhjHeeJxl43WaQM/65Qv4TMpW0C9gHm-VUHXAtDeU9g"
expires:Sun, 26 Oct 2014 00:35:09 GMT
server:GSE
status:200 OK
vary:Referer
vary:X-Origin
vary:Origin
version:HTTP/1.1
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block

这是Google返回的数据:

{
  "kind": "analytics#gaData",
  "id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:91097884&dimensions=ga:city&metrics=ga:sessions&start-date=14daysAgo&end-date=yesterday",
  "query": {
    "start-date": "14daysAgo",
    "end-date": "yesterday",
    "ids": "ga:91097884",
    "dimensions": "ga:city",
    "metrics": ["ga:sessions"],
    "start-index": 1,
    "max-results": 1000
  },
  "itemsPerPage": 1000,
  "totalResults": 47,
  "selfLink": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:91097884&dimensions=ga:city&metrics=ga:sessions&start-date=14daysAgo&end-date=yesterday",
  "profileInfo": {
    "profileId": "91097884",
    "accountId": "54755159",
    "webPropertyId": "UA-54755159-1",
    "internalWebPropertyId": "87741876",
    "profileName": "All Web Site Data",
    "tableId": "ga:91097884"
  },
  "containsSampledData": false,
  "columnHeaders": [{
    "name": "ga:city",
    "columnType": "DIMENSION",
    "dataType": "STRING"
  }, {
    "name": "ga:sessions",
    "columnType": "METRIC",
    "dataType": "INTEGER"
  }],
  "totalsForAllResults": {
    "ga:sessions": "75"
  },
  "dataTable": {
    "cols": [{
      "id": "ga:city",
      "label": "ga:city",
      "type": "string"
    }, {
      "id": "ga:sessions",
      "label": "ga:sessions",
      "type": "number"
    }],
    "rows": [{
      "c": [{
        "v": "(not set)"
      }, {
        "v": "3"
      }]
    }, {
      "c": [{
        "v": "Ashburn"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Atlanta"
      }, {
        "v": "4"
      }]
    }, {
      "c": [{
        "v": "Bari"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Belo Horizonte"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Biloxi"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Birmingham"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Bradenton"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Chandler"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Columbus"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Cordoba"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Dallas"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Eastpoint"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Erlanger"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Fort Bragg"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Foz do Iguacu"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Fresno"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Funchal"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Houston"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Jacksonville"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Lake Oswego"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Lake Worth"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Londrina"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Lynn Haven"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Mar del Plata"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Marianna"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Middletown"
      }, {
        "v": "2"
      }]
    }, {
      "c": [{
        "v": "Miramar Beach"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Montreal"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Navarre"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Panama City"
      }, {
        "v": "9"
      }]
    }, {
      "c": [{
        "v": "Panama City Beach"
      }, {
        "v": "7"
      }]
    }, {
      "c": [{
        "v": "Pecan Grove"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Porto Alegre"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Providence"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Rio de Janeiro"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Sabadell"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Sao Paulo"
      }, {
        "v": "3"
      }]
    }, {
      "c": [{
        "v": "Seria"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Surrey"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Tallahassee"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Tampa"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "The Woodlands"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Toronto"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Virginia Beach"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Wellington"
      }, {
        "v": "1"
      }]
    }, {
      "c": [{
        "v": "Wyndham"
      }, {
        "v": "1"
      }]
    }]
  }
}

如果我在美国地图上尝试也可以做到:

最佳答案
我一直在为EMBED api处理GEO图表,并且遇到了完全相同的问题.在我看来,GEO图表的功能有限,而且您设置的区域与显示的数据无关,而是仅将地图缩放到适当的查看区域.我在查询中添加了过滤器,我认为这对您有用…

"query": {
    "start-date": "14daysAgo",
    "end-date": "yesterday",
    "ids": "ga:91097884",
    "dimensions": "ga:city",
    "metrics": ["ga:sessions"],
    "start-index": 1,
    "max-results": 1000,
    "filters": "ga:region==Florida"
 },

注意过滤器的最后一行,我知道这可能并不理想,但这是我可以使Embed GEO图表正常工作的唯一方法.