javascript - 用于短网址的 Google Analytics

标签 javascript google-analytics google-api google-url-shortener

在 javascript 中,我正在尝试访问谷歌缩短网址的分析数据,for example .我累了'URL Shortener API' ,效果很好,我收到了数据。但是此数据没有一天中的每个小时或一个月中的每一天的分析报告,因为它在 here 上可用。 .作为响应,它具有一些属性,例如“点击次数”和“存储桶”,其中包含我需要的点击次数。检查下图:

enter image description here

但这些属性在我通过“shortener API”收到的数据中不可用。为此,我可能会使用 Google Analytics API。谁能建议我如何使用分析 api 来获取任何缩短 url 的分析?

谢谢

最佳答案

您确定您正确使用了 URL Shortener API 吗?

如果我检查您提供的示例,其中包含您需要的数据,例如过去两个小时(每小时不存在)或过去一天的报告,我可以看到例如:

  • 过去两个小时总共有 6 次点击。
  • 过去一天有 1243 次点击。

如果我尝试使用 URL Shortener API 为相同的短 url 获取相同的数据:

curl -X "GET" "https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo. gl/fbsS&projection=FULL&key=YOUR-API-KEY"

我会得到相同的数据:

{
  "kind": "urlshortener#url",
  "id": "http://goo. gl/fbsS",
  "longUrl": "http://www.google.com/",
  "status": "OK",
  "created": "2009-12-13T07:22:55.000+00:00",
  "analytics": {
    "allTime": /* ... */,
    "month": /* ... */,
    "day": {
      "shortUrlClicks": "1243",
      /* ... */,
    },
    "twoHours": {
      "shortUrlClicks": "6",
      /* ... */,
    }
  }
}

所以我在过去一天有 1243 次点击,过去两个小时有 6 次点击,数据是相同的。

如果您需要随时获取所有数据,您要么必须自己存储数据,要么像您说的那样使用 Google Analytics。

Google Analytics 和短网址在 Analytics 中处理起来可能非常棘手,因为它们会将用户从他们的网站重定向到您的网站,这可能会导致 Analytics 将它们视为“直接”,而不是来自您指定的任何事件(时事通讯、Facebook、推特等)。

您需要标记您的网址以便正确跟踪它们。通常,您需要使用 Google URL Builder为您的网址生成自定义广告系列参数。

Google URL Builder 没有 API,但您可以使用上一个链接中提供的详细信息自行生成有效的 URL,并在非短 URL 的末尾附加部分或全部参数,例如 utm_sourceutm_mediumutm_term

当您的非短网址被正确标记后,​​您就可以使用任何您想要的服务来缩短它们。

要取回数据,您需要使用 Google Analytics API,特别是 Reporting API。

一旦通过身份验证,

var discoveryURL = 'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4';

// Load the API
gapi.client.load(discoveryURL)
  .then(function() {
    // Returns Analytics data.
    gapi.client.analyticsreporting.reports.batchGet({
      "reportRequests": [
        {
          "viewId": VIEW_ID, 
          // View IDs can be fetched from the Analytics Account Explorer
          // https://ga-dev-tools.appspot.com/account-explorer/
          "dateRanges": [
            {
              "startDate": "7daysAgo",
              "endDate": "today"
            }
          ],
          "metrics": [
            {
              "expression": "ga:sessions"
            }
          ]
        }
      ]
    })
    .then(function(response) {
      var json = JSON.stringify(response.result, null, 2);

      // Do anything you want with the JSON returned.
    });
  });

这里使用的主要函数是batchGet,您可以获取有关您可以在Reporting API v4 reference 上使用的字段和选项的所有信息。 .

您将能够玩弄各种字段,例如日期 ( DateRange )、dimensions等,以获取您在应用程序中需要的所有数据。

关于javascript - 用于短网址的 Google Analytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192805/

相关文章:

javascript - D3js : mouseover of one element change opacity of several others elements

google-api - 谷歌验证 API 503

javascript - Neo4j 和 Neovis.js

javascript - Google Analytics - 跟踪用户调查问卷分数

r - 在 Shiny 的应用程序中嵌入 Google 分析跟踪代码

javascript - 将电话记录记录到 Google Analytics

javascript - 通过 JavaScript 中的 GDrive REST API 创建新电子表格?

php - 谷歌网站管理员 API : How to impersonate using server to server request?

javascript - 我可以使用 Google Drive 进行 Chrome 扩展(而不是应用程序)吗

javascript - jQuery window.resize() 中的错误?