使用ASP.NET覆盖Google Analytics _trackPageView的方法 [英] Approaches to overriding Google Analytics _trackPageView using ASP.NET

查看:135
本文介绍了使用ASP.NET覆盖Google Analytics _trackPageView的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Analytics的网站上工作。在大多数页面上,默认调用_trackPageView()来记录GA中使用当前URL的页面视图工作得很好。我可以将该代码添加到母版页,以便将其包含在每个页面中。然而,在某些情况下,我想覆盖GA中记录的页面的名称。例如,我可能只有一个页面,它有多个步骤,并且希望将它们记录为GA中的单独页面。 _trackPageView方法对此很好,但我不确定将它合并到ASP.NET中的最佳方式。我不想处理在每个页面上调用_trackPageView的逻辑。



我在想要在母版页上公开一个属性如OverrideGAURL),如果存在的话插入到对_trackPageView的调用中,或者将GA脚本渲染包装到一个用户控件中,该控件具有暴露的方法或属性以覆盖默认呈现。



有没有做过类似的事情,或者有什么建议来解决这个问题?我的主要目标是封装GA脚本渲染,并且能够在后面的代码中仅用一行代码覆盖页面名称,或者设置一个属性。

解决方案

结束使用公共P​​ageName属性创建一个简单的用户控件。如果PageName属性被设置,那么_trackPageView被调用并且PageName值被传递,否则_trackPageView被调用而没有任何参数。



这种方法工作得很好,通过从用户控件中的web.config中读取它来管理环境之间的GA跟踪代码。


I'm working on a site that uses Google Analytics. On most of the pages the default call to _trackPageView() that records the page view in GA using the current URL works just fine. I can add that code to a master page so it is included on each page.

However, under certain circumstances I want to override the name of the page that gets recorded in GA. For instance I may have a single page that has multiple steps and want to record them as separate pages in GA. The _trackPageView method works fine for this but I'm not sure of the best way to incorporate it into ASP.NET. I don't want to handle the logic of rendering the call to _trackPageView on each page.

I'm thinking that I either expose a property on the master page (something like OverrideGAURL) that if present is inserted into the call to _trackPageView or wrap the GA script rendering into a user control that has a method or property exposed to override the default rendering.

Has any done something similar or have suggestions on the best way to approach this? My main goal is to encapsulate the GA script rendering and be able to override the page name with just one line of code in the code behind or by setting an attribute.

解决方案

Ended up creating a simple user control with a public PageName property. If the PageName property is set then _trackPageView is called and the PageName value is passed otherwise _trackPageView is called without any parameters.

This approach is working pretty well and made it really easy to manage the GA tracking code between environments by reading it from the web.config within the user control.

这篇关于使用ASP.NET覆盖Google Analytics _trackPageView的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆