I work at the moment with google-analytics when requested, it dynamically sends me an answer. If I am not mistaken, this is a JSON file

Here you can look at the code with which I work. I just downloaded all the html and changed only the client ID there and added "dimensions":"ga:country" after this code "metrics":"ga:users", and now when updating the page it gives me an object in which it displays how many people came to the site and from which region .

But here the snag is that it gives me a code, and I need to take certain objects and their properties from this code and show them on the page as normal text.

Here is the code that google-analytics api returns to me

 { "kind": "analytics#gaData", "id": "тут мой ID", "query": { "start-date": "30daysAgo", //30 дневная статистика "end-date": "today", //последная дата сегодня "ids": "ga:172427234", "dimensions": "ga:country", //регионы "metrics": [ "ga:users" //user-ы ], "start-index": 1, "max-results": 1000 }, "itemsPerPage": 1000, "totalResults": 7, "selfLink": "тут ссылка какая то", "profileInfo": { "profileId": "172427234", "accountId": "116539423", "webPropertyId": "UA-116539423-1", "internalWebPropertyId": "172991088", "profileName": "Все данные по веб-сайту", "tableId": "ga:172427234" }, "containsSampledData": false, "columnHeaders": [ { "name": "ga:country", "columnType": "DIMENSION", "dataType": "STRING" }, { "name": "ga:users", "columnType": "METRIC", "dataType": "INTEGER" } ], "totalsForAllResults": { "ga:users": "43523" //все user-ы которые зашли на сайт }, "rows": [ // регионы из которых зашли на сайт и сколько user-а зашел с каждого региона [ "Dubai", "28961" ], [ "Germany", "1400" ], [ "Netherlands", "111" ], [ "Poland", "10" ], [ "Russia", "1021" ], [ "Ukraine", "28" ], [ "United States", "11992" ] ] } 

From this code I need to extract from the "totalsForAllResults": value of the property "ga:users": that is, the number that it yields 43523 or any other number

Also the name of each country and the number of users (in a word, all the properties located in the row array)

Please help.

1 answer 1

If we imagine that the response - the result of the request.

const r = JSON.parse (response); const needed = r.totalsForAllResults ['ga: users'];

  • shas check and accept an answer if it works - Demon __ ANT