简体   繁体   English

如何从.Net代码背后的数据获取数据到混合面板(JavaScript)

[英]How to get data from .Net code-behind into mixpanel (JavaScript)

All of the examples I have seen are hard coded. 我看到的所有示例都是硬编码的。 Like: 喜欢:

mixpanel.people.identify("12148");

How do I replace the 12148 with a value from the server side? 如何用服务器端的值替换12148?

Ok. 好。 After much experimentation I developed my own answer, and thought I would share it so any other "noobs" with the same problem would have a resource... 经过大量的实验,我得出了自己的答案,并认为可以与他人分享,以便其他遇到相同问题的“菜鸟”都可以得到资源...

First - Retrieve the data from the database (on the server side) and put it in hidden fields (on the client side). 首先-从数据库(在服务器端)检索数据,并将其放在隐藏的字段(在客户端)中。 I did it like this:(in the page load event)(example is in VB) 我这样做是这样的:(在页面加载事件中)(示例在VB中)

Dim User As New UserViewModel(Utilities.CurrentUserID) 
hdnUserID.Value = User.UserId
hdnEmail.Value = User.Email.ToString()
hdnFirstName.Value = User.FirstName.ToString()
hdnLastName.Value = User.LastName.ToString()
hdnMemberSince.Value = User.CreatedDate.ToString()

Hidden fields on client side look like this: (I put them out of the way at the very bottom of the page) 客户端上的隐藏字段如下所示:(我将其放在页面的最底部)

<asp:HiddenField ID="hdnUserID" runat="server" />
<asp:HiddenField ID="hdnEmail" runat="server" />
<asp:HiddenField ID="hdnFirstName" runat="server" />
<asp:HiddenField ID="hdnLastName" runat="server" />
<asp:HiddenField ID="hdnMemberSince" runat="server" />

Then in the client side Javascript (in the head), refer to the values in the hidden fields using the <% * %> method. 然后,在客户端Javascript(位于头部)中,使用<%*%>方法引用隐藏字段中的值。

<script type="text/javascript">
    mixpanel.track("XXXXXXX.aspx Page Loaded");

    mixpanel.people.identify('<% = mxpUserID.Value %>');

    mixpanel.people.set({
        "UserID": '<% = mxpUserID.Value %>',
        "User Type": "Borrower",
        "$created": new Date(),
        "$last_login": new Date(),
        "$email": '<% = mxpEmail.Value %>',
        "First Name": '<% = mxpFirstName.Value %>',
        "Last Name": '<% = mxpLastName.Value %>',
        "Member Since": '<% = mxpMemberSince.Value %>'
    });
</script>

This is what worked for me. 这对我有用。 MixPanels documentation is lacking (in my opinion) any real help in dot.net implementation. (我认为)MixPanels文档在dot.net实施中缺乏任何真正的帮助。 So there you have it. 所以你有它。 Hope it helps someone, somewhere, sometime. 希望它能在某个时间某处对某人有所帮助。

While this question and answer are tagged mixpanel and pertain to my attempt to use mixpanel, the true basis of the question should have been; 虽然此问题和答案被标记为mixpanel,并且与我使用mixpanel的尝试有关,但问题的真正基础应该是: "How to obtain data from the database, and make it available to JavaScript, in .Net" “如何从数据库中获取数据,并使其可用于.Net中的JavaScript”

Just thought of an even better way. 只是想到了更好的方法。 Use StringBuilder and RegisterStartupScript. 使用StringBuilder和RegisterStartupScript。

Dim User As New UserViewModel(Utilities.CurrentUserID)
Dim strMixCommand As New System.Text.Stringbuilder()

strMixCommand.Append("mixpanel.track('XXXXXX.aspx Page Loaded');")
strMixCommand.Append("mixpanel.people.identify(" & User.UserID & ");"
strMixCommand.Append("mixpanel.people.set({ 'UserID':'" & User.UserID & "',"
strMixCommand.Append("'UserType':'Borrower',"
strMixCommand.Append("'$created':new Date(),"
strMixCommand.Append("'$last_login':new Date(),"
strMixCommand.Append("'$email':'" & User.Email.ToString() & "',"
strMixCommand.Append("'First Name':'" & User.FirstName.ToString() & "',"
strMixCommand.Append("'Last Name':'" & User.LastName.ToString() & "',"
strMixCommand.Append("'Member Since':'" & User.CreatedDate.ToString() & "'})"

Dim strScript As String = "<script type=""text/javascript"" language=""javascript"">" & strMixCommand & "</script>"

Me.ClientScript.RegisterStartupScript(Me.GetType, "Script", strScript)

If this was run on page load, this would generate the JavaScript on the server side, pre-populated with the desired data from the database, without the intermediate step of populating hidden fields, or having any mixpanel code in the front-end at all... 如果此操作在页面加载中运行,则将在服务器端生成JavaScript,并用数据库中的所需数据进行预填充,而无需执行填充隐藏字段的中间步骤,也无需在前端完全使用任何混合面板代码...

Why not use a .NET based server-side integration for some tracking operations instead? 为什么不对某些跟踪操作使用基于.NET的服务器端集成呢?

See Mixpanel.NET for example: 例如,请参见Mixpanel.NET:

It integrates with Mixpanels's API . 它与Mixpanels的API集成在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用c#将服务器端处理的数据从代码隐藏添加到asp.net中.aspx页面中的javascript - How to add server-side processed data from code-behind to javascript in .aspx page in asp.net using c# 从.NET代码隐藏将参数作为文本传递给JavaScript函数 - Pass parameter as text to JavaScript function from .NET code-behind 从C#.NET中的代码隐藏调用Javascript函数 - Call Javascript function from code-behind in C#.NET 将值从JavaScript传递到ASP.NET代码隐藏 - Passing values from JavaScript to ASP.NET code-behind 是否可以从由 javascript 在代码隐藏中设置的 asp.net 标签获取值? - Is it posible to get the value from an asp.net Label, that was set by javascript in the code-behind? 从 javascript 或代码隐藏获取资源 object - Get resource object from javascript or code-behind 我们如何从asp.net中的代码隐藏调用javascript函数? - How we can call javascript function from code-behind in asp.net? Javascript:如何从ASP.NET中的代码隐藏检查布尔值 - Javascript: How to check boolean value from code-behind in ASP.NET 如何在JavaScript设置的代码隐藏中获取文本框的值? - How to get value of textbox in code-behind which was set by Javascript? 如何获取TextBox的文本并将其从JavaScript发送到使用QueryString进行代码隐藏 - How to get text of a TextBox and send it from JavaScript to code-behind using QueryString
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM