2

I have a web application in which I want to track two different views of the same page from google analytic.

From code behind I am managing the two different views..but didn't find the way to manage the below script from code behind.

<script type="text/javascript">
    try {
        var pageTracker = _gat._getTracker("XX-XXXXXX-XX");
        pageTracker._setDomainName(".DOMAIN.com");
        pageTracker._trackPageview();
    } catch (err) { }
</script> 

So how can I change this script dynamically from code behind...?

4

2 回答 2

1

一个技巧是在 aspx 部分放置两个隐藏字段

<asp:HiddenField ID="TrackerCode" runat="server" ClientIDMode="Static">
</asp:HiddenField>
<asp:HiddenField ID="DomainName" runat="server" ClientIDMode="Static">
</asp:HiddenField>

然后在页面加载时分配它们

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TrackerCode.Text = "XX-XXXXXX-XX";
        DomainName.Text = ".DOMAIN.com";
    }
}

最后将您的脚本放在页面底部。(在我们创建的标签之后)

<script type="text/javascript">
    try {
        var tcode = document.getElementById("TrackerCode").value;
        var domain = document.getElementById("DomainName").value;
        var pageTracker = _gat._getTracker( tcode );
        pageTracker._setDomainName( domain );
        pageTracker._trackPageview();
    } catch (err) { }
</script> 
于 2012-08-25T13:02:57.400 回答
0

声明一个函数并在该函数中定义所有逻辑。

并在 DOM 准备好或在 window.onload 时调用该函数。

window.onload=function(){
              //your function called it here or right your code here
}

希望它会起作用

于 2012-08-25T08:10:14.933 回答