跟踪与谷歌分析文本框项 [英] Tracking Textbox Entries with Google Analytics

查看:323
本文介绍了跟踪与谷歌分析文本框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在谷歌捕捉到

分析被输入到一个文本框的数据。在code是我到目前为止低于,我试图通过JavaScript来捕获数据,但我似乎无法得到它的工作。我使用的CMS平台是Sitecore的。

 <%@控制语言=C#AutoEventWireup =真codeBehind =SalesRepSearch.ascx.cs继承=USEndoscopy.Website.layouts.USEndoscopy。 Sublayouts.SalesRepSearch%GT;
<%@导入命名空间=USEndoscopy.Library.Search%GT;<脚本类型=文/ JavaScript的>
    VAR文本=的document.getElementById('txtZip code'),
    提交按钮=的document.getElementById('btnSalesRepSearch');submitButton.onClick =功能(){
    _trackEvent('数据存储',textbox.name,textbox.value,0);
    //数据存储可以用你想要的任何数据类别进行更换,对于sortability的缘故
    // 0可以用你想要的任何其它数据所取代 - 但它必须是数字
}
< / SCRIPT>
&所述p为H.;&下; / P>
&所述p为H.;
    < ASP:小组DefaultButton =btnSalesRepSearch=服务器>
    < ASP:文本框ID =txtZip code=服务器WIDTH =300像素>< / ASP:文本框>
    < ASP:按钮的ID =btnSalesRepSearch=服务器的CssClass =buttonregular文本=搜索的OnClick =btnSalesRepSearch_Click/>
    < ASP:的RequiredFieldValidator ID =reqTxtZip code=服务器的ValidationGroup =邮编code的ControlToValidate =txtZip code显示=动态的ErrorMessage =请输入有效的美国邮编code的CssClass =错误>< / ASP:&的RequiredFieldValidator GT;
        < ASP:RegularEx pressionValidator ID =regExTxtZip code=服务器ValidationEx pression =?^ \\ D {5}( - \\ D {4})$的ValidationGroup =邮编code的ControlToValidate =txtZip code显示=动态的ErrorMessage =请输入有效的美国邮编code的CssClass =错误>< / ASP:RegularEx pressionValidator>
    < / ASP:面板>
&所述; / P>&所述p为H.;
    < ASP:直放站ID =salesRepContainer=服务器>
        <&ItemTemplate中GT;
            <%#((SalesRep的)的Container.DataItem).RepName%GT;< BR />
            <%#((SalesRep的)的Container.DataItem).Phone%GT;< BR />
            &所述; A HREF =电子邮件地址:或其可%#((SalesRep的)的Container.DataItem).Email%​​gt;中与GT;&下;%#((SalesRep的)的Container.DataItem).Email%​​GT;&下; / A> < BR />
        < / ItemTemplate中>
    < / ASP:直放站>
&所述; / P>
&所述p为H.;
    < SC:文本字段=InternationalMessage=服务器/>
&所述; / P>


解决方案

这是因为 ASP.NET 已经改变了 ID 以使它们的时候你的文本框的值。

如果您运行.NET V4或更高然后添加的ClientIDMode =静态你的文本框和
按钮。这告诉.NET不改变控件的ID。

 < ASP:文本框ID =txtZip code=服务器的ClientIDMode =静态WIDTH =300像素>< / ASP:文本框>
    < ASP:按钮的ID =btnSalesRepSearch=服务器的ClientIDMode =静态的CssClass =buttonregular文本=搜索的OnClick =btnSalesRepSearch_Click/>

如果您运行的不到.NET 4中改变你的Javascript功能来:(.ClientID获取.NET给了控制,当它呈现它的ID)

  VAR文本=的document.getElementById('<%= txtZip code.ClientID%GT;'),
    提交按钮=的document.getElementById('<%= btnSalesRepSearch.ClientID%GT;');

更新

您可能有您的JavaScript函数尝试之前到点击函数分配给按钮的问题的你的按钮实际上是可以在DOM的。

所以,请尝试以下操作:

Javascript函数

 函数btnSearchClick(){
     VAR文本=的document.getElementById('txtZip code');
     //改变你的谷歌code到这一点:     _gaq.push(['_ trackEvent',textbox.name,textbox.value,0]);     返回true;
 }

和您的按钮添加属性

 的OnClientClick =返回btnSearchClick()

I am trying to capture in google analytics the data that is entered into a textbox. The code that I have so far is below, I am trying to capture the data by Javascript, but I can't seem to get it to work. The CMS Platform I am using is sitecore.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SalesRepSearch.ascx.cs"     Inherits="USEndoscopy.Website.layouts.USEndoscopy.Sublayouts.SalesRepSearch" %>
<%@ Import Namespace="USEndoscopy.Library.Search" %>

<script type="text/javascript">
    var textbox = document.getElementById('txtZipCode'),
    submitButton = document.getElementById('btnSalesRepSearch');

submitButton.onClick = function () {
    _trackEvent('data-store', textbox.name, textbox.value, 0);
    // 'data-store' can be replaced with whatever category of data you want, for sortability's sake
    // the 0 can be replaced with any other numerical data you want - but it must be numerical
}
</script>
<p></p>
<p>
    <asp:Panel DefaultButton="btnSalesRepSearch" runat="server">
    <asp:TextBox ID="txtZipCode" runat="server" Width="300px"></asp:TextBox>
    <asp:Button ID="btnSalesRepSearch" runat="server" CssClass="buttonregular" Text="Search" OnClick="btnSalesRepSearch_Click" />
    <asp:RequiredFieldValidator ID="reqTxtZipCode" runat="server" ValidationGroup="ZipCode" ControlToValidate="txtZipCode" Display="Dynamic" ErrorMessage="Please enter a valid US Zip code" CssClass="error"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="regExTxtZipCode" runat="server"     ValidationExpression="^\d    {5}(-\d{4})?$" ValidationGroup="ZipCode" ControlToValidate="txtZipCode" Display="Dynamic"     ErrorMessage="Please enter a valid US Zip code" CssClass="error"></asp:RegularExpressionValidator>
    </asp:Panel>
</p>

<p>
    <asp:Repeater ID="salesRepContainer" runat="server">
        <ItemTemplate>
            <%# ((SalesRep)Container.DataItem).RepName %><br />
            <%# ((SalesRep)Container.DataItem).Phone %><br />
            <a href="mailto:<%# ((SalesRep)Container.DataItem).Email %>"><%# ((SalesRep)    Container.DataItem).Email %></a><br />
        </ItemTemplate>
    </asp:Repeater>
</p>
<p>
    <sc:Text Field="InternationalMessage" runat="server" />
</p>

解决方案

It's because ASP.NET has changed the ID value of your textboxes at the time of rendering them.

If your running .net V4 or greater then add ClientIDMode="Static" to your textbox and button. This tells .net to not change the ID of your control.

    <asp:TextBox ID="txtZipCode" runat="server" ClientIDMode="Static" Width="300px"></asp:TextBox>
    <asp:Button ID="btnSalesRepSearch" runat="server" ClientIDMode="Static" CssClass="buttonregular" Text="Search" OnClick="btnSalesRepSearch_Click" />

If your running less than .net 4 change your Javascript to: (.ClientID gets the ID that .net gave the control when it rendered it)

    var textbox = document.getElementById('<%= txtZipCode.ClientID %>'),
    submitButton = document.getElementById('<%= btnSalesRepSearch.ClientID %>');

UPDATE

You might be having an issue where your javascript function is trying to assign a click function to your button before your button is actually available on the DOM.

So try the following:

Javascript function

function btnSearchClick(){
     var textbox = document.getElementById('txtZipCode');


     // Change your google code to this:

     _gaq.push(['_trackEvent', textbox.name, textbox.value,  0]);

     return true;
 }

And on your button add the property

 onClientClick="return btnSearchClick()"

这篇关于跟踪与谷歌分析文本框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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