我希望更准确地跟踪将流量发送到我的asp.net网站的营销计划。目前,我已经编写了单独的页面来查找引用查询字符串参数" gclid"。
示例:http://example.com/landingpage.aspx?gclid=[vlue]
我希望有一种方法可以让我的网站中的任何目标网页实现此流程全局,并且只有在目标网页的查询字符串中找到时,才会将Cookie设置为等于gclid的值。
这是用Session_OnStart可靠地完成的吗?
答案 0 :(得分:1)
是的,Session_Start是一种可行的方法。
此时HttpContext.Current
有效,因此您可以使用HttpContext.Current.Request.QueryString
到达查询字符串,即
var gclid = HttpContext.Current.Request.QueryString["gclid"];
答案 1 :(得分:1)
Session_Start
文件中的Global.asax
事件是您可以使用的另一种选择。请参阅@ sh1rts的回答。
但Session_Start
事件(当然)仅在启动新会话时触发。假设情况:
Session_Start
运行并将gclid
值存储到新会话Session_Start
如果gclid
第二次不同,则不会使用该值更新会话。这在实践中可能不是问题,因此Session_Start
可能是一个解决方案。如果此可能成为问题,那么您可以在Global.asax
中使用针对每个请求运行的其他事件。例如Application_PostAcquireRequestState
:
void Application_PostAcquireRequestState(object sender, EventArgs e)
{
var httpApp = sender as HttpApplication;
if(httpApp != null && httpApp.Context != null && httpApp.Context.Session != null)
{
if(!string.IsNullOrEmpty(httpApp.Context.Request.QueryString["gclid"]))
httpApp.Context.Session["gclid"] = httpApp.Context.Request.QueryString["gclid"];
}
}