如何在Angular 2中安装Woopra跟踪代码

时间:2017-03-16 22:04:09

标签: angular woopra

我想知道哪个是在Angular 2应用程序上安装Woopra跟踪代码的最佳方式。我已将跟踪代码添加到 index.html

<script>
    (function(){
        var t,i,e,n=window,o=document,a=arguments,s="script",r=["config","track","identify","visit","push","call","trackForm","trackClick"],c=function(){var t,i=this;for(i._e=[],t=0;r.length>t;t++)(function(t){i[t]=function(){
            return i._e.push([t].concat(Array.prototype.slice.call(arguments,0))),i}})(r[t])};for(n._w=n._w||{},t=0;a.length>t;t++)n._w[a[t]]=n[a[t]]=n[a[t]]||new c;i=o.createElement(s),i.async=1,i.src="//static.woopra.com/js/w.js",e=o.getElementsByTagName(s)[0],e.parentNode.insertBefore(i,e)
    })("woopra");
    woopra.config({domain: 'My.Domain.com.'});
    woopra.track();
</script>

我可以通过window["woopra"]访问所有需要组件的全局变量 woopra ,但我想知道是否更好地将代码添加到某些配置文件中,例如 webpack。 common.js 然后导出变量。

有人知道更好的方法吗?感谢

1 个答案:

答案 0 :(得分:0)

Woopra节点包已有一个类型定义文件:

https://github.com/Woopra/node-woopra/blob/master/index.d.ts

不幸的是,最新的npm包看起来已经过时了(v0.1.1)。

所以...首先更新到最新版本:

更新到0.2.0版(github上的最新版本):

npm update woopra@0.2.0

第二件不幸的是,他们并没有将index.d.ts作为npm包的一部分。

所以我建议从这里复制index.d.ts:

https://github.com/Woopra/node-woopra/blob/master/index.d.ts

并将其放在源目录中的某个位置:

src/woopra.d.ts

然后将类型定义添加到包中。

<强>的package.json

{
    typings: 'index.d.ts'; // this is **your** main d.ts file
}

index.d.ts (对于您的包裹)

import * from '...'; // your other type definitions
import * from './<path_to_src>/woopra.d.ts';

哇!这比应该更难...希望作者更新npm包,以便部署d.ts文件。它应该工作......但也许它是一个正在进行的工作,并且可能有一个原因,他们没有包含d.ts文件。如果是这样,可能会在Github上提出一个问题吗?