我有一个允许用户下载文件的Web应用程序。我动态生成了href="xx"
个链接。
我已经看过这篇文章,解释了如何使用Angular跟踪路线:http://www.arnaldocapo.com/blog/post/google-analytics-and-angularjs-with-ui-router/72
如何跟踪用户下载的每个文件?
答案 0 :(得分:1)
我不熟悉Google分析如何运作的细节,但您可以通过在中间攻击中创建自己的人(只是没有任何恶意)来跟踪用户下载(或任何其他操作)。
只需在下载链接中添加一个ng-click
即可启动记录项目下载地点的承诺
<a href="..." ng-click="log()">Click here to download!</div>
$scope.log = function(){
//$http post or whatever you want
//Then resume normal click functionality
}
答案 1 :(得分:1)
您想使用ng-click进行此操作。我还会使用GA的事件跟踪来跟踪此指标。这是一个例子:
<a href="#" ng-click="logDownload()">Download</a>
$scope.logDownload = function () {
$window.ga('send', 'event', 'Download', 'File Name'); //This would log a GA Event
$location.path('your path to download the file'); //use this if you are linking to an angular route
};
使用将$ window和$ location注入控制器。使用$ window来保持代码可测试。
答案 2 :(得分:1)
最后,这就是我解决它的方式,感谢newg和David Grinberg。
我没有使用$ location.path()解决方案,因为我希望链接在新窗口中打开,目标为&#39; _blank&#39;
HTML
<a href='{{ item.url }}' target='_blank' ng-click='trackDownload(item)'>
{{ item.name}}
</a>
控制器
$scope.trackDownload = function(item) {
if (!$window.ga) return;
$window.ga('send', 'event', 'download', item.full);
};