gpt4 book ai didi

javascript - AngularJS 和 google 分析,站点 id 作为常量存储在根范围内

转载 作者:行者123 更新时间:2023-11-28 13:29:15 26 4
gpt4 key购买 nike

我刚刚使用 Angular 的 yeoman 生成器创建了一个新应用程序,我可以看到 google Analytics 已插入到 index.html 中。

我已向我的应用程序添加了一个配置常量,并通过以下方式将其提供给 rootscope:

angular.module(... ['config',...])
.run(function ($rootScope,config) {
$rootScope.config = config
});

我的配置模块如下所示

angular.module('config', [])
.constant('config', {analytics:{siteId:'UA-12345-1'}})

现在我可以将代码更改为以下内容,以便插入和使用分析站点 ID 吗?或者是否会在 Handlebars 之间的内容之前评估 javascript 代码 {{config.analytics.siteId}}

<!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
<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');

ga('create', '{{config.analytics.siteId}}');
ga('send', 'pageview');
</script>

我似乎无法检测到任何对分析服务的调用,我猜测会有 ajax 请求?但这可能是因为我的应用程序是通过本地主机访问的?

在这种情况下我怎样才能达到预期的行为?干杯!

最佳答案

使用$injector.get()。

您的问题的答案是否定的,您的猜测是正确的,JavaScript 计算 ga() 函数并使用带括号的字符串参数。

更新

如果我是你,我会使用 module.run()。

run(function(constValue){

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

ga('create', constValue);
ga('send', 'pageview');

})

关于javascript - AngularJS 和 google 分析,站点 id 作为常量存储在根范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26400761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com