与React.js集成?

时间:2016-10-18 15:52:03

标签: google-analytics google-analytics-v4

我试图将Google Embed API与React.js集成,我在componentDidMount中添加了以下内容

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

    this.analytics();

    gapi.analytics.ready(function() {

        gapi.analytics.auth.authorize({container: 'embed-api-auth-container', clientid: <google.clientID>});

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

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

        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%'
                }
            }
        });

        viewSelector.on('change', function(ids) {
            dataChart.set({
                query: {
                    ids: ids
                }
            }).execute();
        });

    });

但是,我在控制台

中收到以下错误

未捕获的TypeError:window.google.load不是函数(...)

我错过了什么?

0 个答案:

没有答案