【问题标题】:Google Analytics embed api not working on Angular 2 appGoogle Analytics 嵌入 API 不适用于 Angular 2 应用程序
【发布时间】:2017-09-05 11:09:45
【问题描述】:

我正在使用以下代码将 Google Analytics api 嵌入到我的 Angular 2 应用程序中:

我的组件.html

  <div id="embed-api-auth-container"></div>
  <div id="chart-container"></div>
  <div id="view-selector-container"></div>

<script>

  gapi.analytics.ready(function() {

    /**
     * Authorize the user immediately if the user has already granted access.
     * If no access has been created, render an authorize button inside the
     * element with the ID "embed-api-auth-container".
     */
    gapi.analytics.auth.authorize({
      container: 'embed-api-auth-container',
      clientid: My-Client-Id
    });


    /**
     * Create a new ViewSelector instance to be rendered inside of an
     * element with the id "view-selector-container".
     */
    var viewSelector = new gapi.analytics.ViewSelector({
      container: 'view-selector-container'
    });

    // Render the view selector to the page.
    viewSelector.execute();


    /**
     * Create a new DataChart instance with the given query parameters
     * and Google chart options. It will be rendered inside an element
     * with the id "chart-container".
     */
    var dataChart = new gapi.analytics.googleCharts.DataChart({
      query: {
        metrics: 'ga:sessions',
        dimensions: 'ga:date',
        'start-date': '30daysAgo',
        'end-date': 'yesterday'
      },
      chart: {
        container: 'chart-container',
        type: 'LINE',
        options: {
          width: '100%'
        }
      }
    });


    /**
     * Render the dataChart on the page whenever a new view is selected.
     */
    viewSelector.on('change', function(ids) {
      dataChart.set({query: {ids: ids}}).execute();
    });

  });
</script>

将 My-Client-Id 替换为 Google 客户端 ID,并在 index.html 中添加加载 platform.js 代码,如下所示:

     <script>
    (function(w,d,s,g,js,fs){
      g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
      js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
      js.src='https://apis.google.com/js/platform.js';
      fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
    }(window,document,'script'));
  </script>

我在基于 jQuery 的应用程序中测试了相同的代码,该代码运行良好,但它在 Angular2 应用程序中显示空白页面,没有 JavaScript 控制台错误。

我也已将该网站添加到授权的 JavaScript 来源。 Angular2 应用还需要做其他配置吗?

供您参考,我创建了 oAuth 客户端 ID,启用了 Analytics API,将网站添加到授权的 JavaScript 源并禁用了我的广告拦截器。

【问题讨论】:

  • 你找到这个了吗?我有一个类似的设置,并试图弄清楚如何让它在 angular 4 中工作。如果你把它作为一个独立的东西,它就可以工作,但我不能让它工作......跨度>
  • 你管理好了吗?我也遇到了同样的问题

标签: angular google-analytics google-analytics-api


【解决方案1】:

组件HTML文件中的脚本标签为automatically stripped by angular

因此,您需要在 ngOnInit() 函数中将 gapi.analytics.ready 函数移动到 Mycomponent.ts 文件中。

index.html文件里的script标签没问题,可以保留。

【讨论】:

    猜你喜欢
    • 2018-06-10
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2017-03-20
    相关资源
    最近更新 更多