1

我正在基于热毛巾模板构建应用程序。Google Analytics 遇到问题。我对此完全陌生,这是我到目前为止所做的:

1)在谷歌分析帐户中注册

2)在我粘贴的结束标记之前的 index.cshtml 文件中:

 <script>
    (function (i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date(); a = s.createElement(o),
        m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
    })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
</script>

3) 将 TypeScript 定义从 nuget 添加到 shell.ts 文件(我将其重写为 TypeScript)

/// <reference path="../../scripts/typings/google.analytics/ga.d.ts" />

版本为 0.0.9。

后期激活方法:

        this.router.on('router:navigation:complete', (instance, instruction) => {
         ga('create', 'UA-XXXXX-1', { 'cookieDomain': 'none' });
         ga('send', 'pageview');
    });

但无法编译文件 - ga.d.ts 中的 ga 对象定义没有声明任何函数 - 只有属性。

我做了一些研究并找到了可行的解决方案:

        var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-51465809-1']);
    _gaq.push(['_setCookieDomain', 'none']);
    _gaq.push(['_gat._anonymizeIp']);
    _gaq.push(['_trackPageview']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_setAllowHash', false]);
    _gaq.push(() => {
        var tracker = _gat._getTrackerByName('UA-51465809-1');
        tracker._trackPageview();
    });

但它似乎适用于以前的谷歌分析版本——不是最新的。

试图使用站点http://petermorlion.blogspot.com/2014/04/using-google-analytics-in-durandal-spa.html的信息但没有成功 - ga.d.ts 中的 ga 对象定义没有t 没有声明任何函数。

任何帮助表示赞赏。

4

2 回答 2

0

你确定你的激活方法是正确的?d.ts 文件https://github.com/borisyankov/DefinitelyTyped/blob/master/google.analytics/ga.d.ts似乎与这里提到的一致https://developers.google.com/analytics/devguides /collection/gajs/目前

于 2014-05-29T21:59:45.237 回答
0

这是我在 shell.ts 中的激活逻辑:

 activate = () => {
    ko.validation.configure({
        registerExtenders: true,
        messagesOnModified: false,
        decorateElement: true,
        errorElementClass: 'error',
        messageTemplate: 'errorMessageTemplate',
        errorMessageClass: 'error',
        decorateInputElement: true
    });



    return m_dataservice.DataServiceInstance.primeData()
        .then(this.boot)
        .fail(this.failedInitialization);
}

boot = () => {
    m_dataservice.DataServiceInstance.getHelpLink().then((data) => { this.helpLink = data.results; });
    m_dataservice.DataServiceInstance.getFooterLinks().then((data) => {
        this.tmpLinki(data.results);
    });


    var routes = [
        { route: '', moduleId: 'home', title: 'My Apps', nav: true, customBind: false },
        { route: 'appcatalog', moduleId: 'appcatalog', title: 'App Catalog', nav: true, customBind: false },
        { route: 'aboutus', moduleId: 'aboutus', title: 'About Us', nav: true, customBind: false },
        { route: 'faq', moduleId: 'faq', title: 'FAQ', nav: true, customBind: false },
        { route: 'login', moduleId: 'login', title: 'Login', nav: true, customBind: false }
    ];


    this.router.on('router:route:not-found', function(fragment) {
        this.logError('No Route Found', fragment, true);
    });

    this.router.on('router:navigation:complete', (instance, instruction) => {
        ga('create', 'UA-XXXX-Y', { 'cookieDomain': 'none' });
        ga('send', 'pageview');
    });



   var tmp = this.router.makeRelative({ moduleId: 'viewmodels' }) 
       .map(routes) 
       .buildNavigationModel(); 
   return tmp.activate(); 

}

以“ga”开头的行带有红色下划线—— “不能调用类型缺少调用签名的表达式”。

于 2014-05-30T08:49:03.740 回答