0

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.

4

3 回答 3

2

我不是 ASP.NET 人,但使用_trackPageView生成额外的虚拟页面视图只能通过一种方式完成:使用适当的 URL 路径调用该方法。在包含多个步骤的页面上,您必须_trackPageView为每个步骤调用,如下所示:

pageTracker._trackPageView('/Page_name/Step2');

除此之外,您仍然需要_trackPageView标准跟踪代码中的原始调用。

请参阅如何跟踪 AJAX 应用程序?在 Google Analytics 帮助中了解更多详细信息。

于 2009-06-04T21:08:41.860 回答
1

最终创建了一个具有公共 PageName 属性的简单用户控件。如果设置了 PageName 属性,则调用 _trackPageView 并传递 PageName 值,否则不带任何参数调用 _trackPageView。

这种方法运行良好,并且通过从用户控件中的 web.config 读取它来管理环境之间的 GA 跟踪代码变得非常容易。

于 2009-07-01T01:39:11.863 回答
0

您最好让 Google 使用多视图跟踪标准页面名称,如果您不希望它出现在您的报告中,只需使用 Google Analytics 过滤掉页面名称。然后,在每个视图中,只需将此脚本放入: pageTracker._pageView('/UniqueViewName'); 跟踪每个步骤,但您希望随后显示在您的内容中。

要了解过滤页面,请参阅:https ://www.google.com/support/googleanalytics/bin/answer.py?answer=55593&hl=en_US&utm_id=ad

于 2009-06-17T18:08:54.157 回答