简体   繁体   English

用Javascript处理返回的Google Analytics(分析)API数据

[英]Manipulating returned Google Analytics API data in Javascript

I'm trying to use the Google Analytics Embed API to create text-based statements on KPI-performance, to sit above a dashboard. 我正在尝试使用Google Analytics(分析)Embed API来创建有关KPI性能的基于文本的语句,并将其放置在仪表板上。 An example statement might be something like: "There were X online transactions this week, which was a change of Y% on last week's performance, this came from Z sessions giving an approximate value-per-session of $abc". 一个示例语句可能是这样的:“本周有X笔在线交易,与上周的表现相比发生了Y%的变化,这来自Z会话,每会话的近似价值为$ abc”。 And so on.. The intent is that this would be useful insights from the data that, could be automatically generated and should take less space on a page than say a graph or chart. 这样做的目的是,这将是有用的数据见解,这些数据可以自动生成,并且在页面上所占的空间比图表或图表要少。

To achieve this I need to be able to: 为此,我需要能够:

  1. Query the API 查询API
  2. Manipulate the returned results/perform basic calculations 处理返回的结果/执行基本计算
  3. Send these results to my HTML page 将这些结果发送到我的HTML页面

Seems I am able to do steps 1 and 3. I can perform queries on the API and send these to my HTML page but I am stuck on step 2 - manipulating / changing the returned results in the way I want to. 似乎我能够执行第1步和第3步。我可以在API上执行查询,然后将其发送到我的HTML页面,但是我却陷入了第2步-按照我想要的方式操作/更改返回的结果。

Taking a real example - let's say I want to show week-on-week change in my session data. 举一个真实的例子-假设我要在会话数据中显示每周的变化。 I can get the last 7 days worth of data and send these to the element (box1) on my page using the following code: 我可以使用以下代码获取最近7天的数据并将其发送到页面上的元素(box1):

 var lastSevenDays = new gapi.analytics.report.Data({
      query: {
     ids: 'ga:123456789',
          'start-date':'7daysAgo',
          'end-date':'today',
          'metrics': 'ga:sessions'
        }
       });

       lastSevenDays.on('success', function(lastSevenDays) {
             for (var prop in lastSevenDays) {
              var outputDiv = document.getElementById('box1');
              outputDiv.innerHTML = lastSevenDays[prop];   
         }  
            console.log(lastSevenDays);

    }); 
  lastSevenDays.execute();

  //profile id: 123456789 isn't real in this example. 

This works fine and session data for the last 7 days is written to my page. 效果很好,最近7天的会话数据已写入我的页面。

However, when I try and combine this with my second query, the data from the previous 7 days, send this to the page as box2 (second query) and finally box3 (difference in the numbers) something doesn't work. 但是,当我尝试将其与第二个查询结合使用时,则将前7天的数据作为box2(第二次查询)发送到页面,最后以box3(数字上的差异)发送到页面,这是行不通的。

Code attempt below. 下面的代码尝试。 I'm thinking it could be possible my first query is returning an array from the Google Analytics API, which when outputted to a HTML page seems to work, since there is only one value in the array. 我在想我的第一个查询可能会从Google Analytics(分析)API返回一个数组,当输出到HTML页面时,该数组似乎可以工作,因为数组中只有一个值。

  var lastSevenDays = new gapi.analytics.report.Data({
  query: {
    ids: 'ga:123456789',
      'start-date':'7daysAgo',
      'end-date':'today',
      'metrics': 'ga:sessions'
    }
   });  
   lastSevenDays.on('success', function(lastSevenDays) {
         for (var prop in lastSevenDays) {
          var outputDiv = document.getElementById('box1');
          outputDiv.innerHTML = lastSevenDays[prop];   
         }  
            console.log(lastSevenDays);             
    }); 
  lastSevenDays.execute();

  // The previous 7 days 

  var previousSevenDays = new gapi.analytics.report.Data({
  query: {
    ids: 'ga:123456789',
      'start-date':'7daysAgo',
      'end-date':'today',
      'metrics': 'ga:sessions'
    }
   });  
   previousSevenDays.on('success', function(previousSevenDays) {
         for (var prop in previousSevenDays) {
          var outputDiv = document.getElementById('box2');
          outputDiv.innerHTML = previousSevenDays[prop];   
         }  
            console.log(previousSevenDays);

    }); 
  previousSevenDays.execute();

  // Week on week change, where I am going wrong:

  var weekOnWeekChange = lastSevenDays - previousSevenDays; 
     weekOnWeekChange.on('success', function(weekOnWeekChange) {
         for (var prop in weekOnWeekChange) {
          var outputDiv = document.getElementById('box3');
          outputDiv.innerHTML = weekOnWeekChange[prop];   
         }  
            console.log(weekOnWeekChange);

    }); 
  weekOnWeekChange.execute();


  //profile id: 123456789 isn't real in this example. 

I'm a marketeer just trying to learn a bit more, not a coder. 我是一个营销人员,只是想学一点点东西,而不是编码员。 Therefore I am not too familiar with Javascript. 因此,我不太熟悉Javascript。 So I really appreciate the advice of anyone who can point me in the right direction. 因此,我非常感谢任何能为我指明正确方向的人的建议。

So please excuse any massive errors of mine and thank you in advance for any help. 因此,请原谅我的任何重大错误,并在此先感谢您的帮助。

I would start by adding some logging 我将从添加一些日志开始

....
'success', function(weekOnWeekChange) {
   console.log(weekOnWeekChange);

Use cmd+alt+j (on a mac) to open the console. 使用cmd + alt + j(在Mac上)打开控制台。 then you will be able to see what is being returned by the functions 然后您将能够看到函数返回的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM