我见过的所有例子都是硬编码的。 像:
mixpanel.people.identify("12148");
如何使用服务器端的值替换12148?
答案 0 :(得分:0)
确定。经过多次实验,我开发了自己的答案,并认为我会分享它,所以任何其他具有相同问题的“新手”都会有资源......
首先 - 从数据库中检索数据(在服务器端)并将其放在隐藏字段中(在客户端)。我是这样做的:(在页面加载事件中)(例如在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()
客户端的隐藏字段如下所示:(我将它们放在页面底部的位置)
<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" />
然后在客户端Javascript(在头部),使用&lt;%*%&gt;参考隐藏字段中的值。方法
<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>
这对我有用。 MixPanels文档缺乏(在我看来)dot.net实现中的任何实际帮助。所以你有它。希望它能在某个地方帮助某个人。
虽然这个问题和答案都标记为mixpanel,并且与我尝试使用mixpanel有关,但问题的真正基础应该是; “如何从数据库中获取数据,并使其可用于.Net中的JavaScript”
答案 1 :(得分:0)
想到了更好的方法。使用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)
如果这是在页面加载时运行的,这将在服务器端生成JavaScript,预先填充来自数据库的所需数据,而无需填充隐藏字段的中间步骤,或者在前面有任何mixpanel代码 - 结束......
答案 2 :(得分:0)