2

自分のサイトのオンライン訪問者数を知りたい。私は自分の調査を行い、2つの解決策を見つけました。

出典:
ASP.NETのCodeProjectOnlineアクティブユーザーカウンター

セットアップと使用は簡単ですが、すべてのAjaxリクエスト/レスポンスのユーザー数も増えます。私のホームページだけでも12のAjaxリクエストがあります(1つのページに8つのリクエスト、別のページに4つのリクエスト)。これにより、ユーザー数が劇的に増加します。

出典:Stack Overflow Q / A
訪問者数を数える これは、前のものとまったく同じように機能します。

出典:ASP.Netフォーラム C#を使用して「オンラインのユーザー」を確認する方法

これは前の2つより良く見えます。このソリューションの詳細コードは次のとおりです。

void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        HttpContext.Current.Application["visitors_online"] = 0;
    }

void Session_Start(object sender, EventArgs e) 
    {
        Session.Timeout = 20; //'20 minute timeout
        HttpContext.Current.Application.Lock();
        Application["visitors_online"] = Convert.ToInt64(HttpContext.Current.Application["visitors_online"]) + 1;
        HttpContext.Current.Application.UnLock();
    }

void Session_End(object sender, EventArgs e) 
    {
        HttpContext.Current.Application.Lock();
        Application["visitors_online"] = Convert.ToInt64(HttpContext.Current.Application["visitors_online"]) - 1;
        HttpContext.Current.Application.UnLock();
    }

すべてのAjax応答のカウントの増加を無視できるようですが、それでもページの更新またはページ要求ごとに合計されます。

ASP.Netのオンライン訪問者の正確な数を数える方法はありますか?

4

6 に答える 6

5

完全に正確な数値を取得することはできません。ユーザーが「別のサイトに移動した」(そして自分のサイトを離れた)こと、またはそのユーザーが「ブラウザを閉じた」ことを(確実に)検出する方法はありません。

Session_Start / Session_Endの方法には、Session_Endが「InProc」セッションに対してのみ呼び出され、セッションがStateServerまたはSqlServerに格納されている場合には呼び出されないという問題があります。

あなたができるかもしれないこと:

  • Dictionary<string, DateTime>アプリケーションスコープでを保持します。この保存されたセッションID(文字列キー)と最新のアクセス時間(DateTime値)
  • 有効なセッションを持つリクエストごとに、ディクショナリでセッションエントリを見つけて、最新のアクセス時刻を更新します(見つからない場合は新しいエントリを追加します)
  • オンラインユーザーの数を取得する場合は、最初にディクショナリ内のすべてのエントリをループし、セッションタイムアウトが経過したアイテムを削除します。残りの数はオンラインユーザーの数です。

1つの問題(少なくとも):1人のユーザーが2つのブラウザーを同時に使用する場合、2つのセッションが開いており、2倍にカウントされます。ユーザーが常にログインしている場合は、session-idではなくlogin-idを使用できます。

于 2012-07-09T10:41:57.200 に答える
1

まったく別のものを提案したいのですが、これにはGoogleAnalyticを使用してみてください。これらの人々は、オンライン統計を今すぐ追跡できるベータ機能を追加します。

編集:あなたは私を取得しません。私は標準のGoogleAnalytic機能ではないと述べました。新機能であるリアルタイムについて説明しました。詳細については、次の記事を確認してください-現在、サイトで何が起こっていますか?またはこのビデオを見る-GoogleAnalyticsリアルタイムベータ

于 2012-07-11T05:19:01.027 に答える
0

ハンスが述べたように、おそらくセッション状態をStateServerまたはに切り替える必要がありますSQLServerが、これはコードのスニペットでできることではありません。この問題を適切に処理するcodeprojectに関する非常に信頼性の高い詳細な記事があります。

于 2012-07-09T11:04:12.160 に答える
0

これは、ハンス・ケスティングの提案に従って実装したソースコードです。

Global.asax

void Session_Start(object sender, EventArgs e) 
    {
        System.Collections.Generic.Dictionary<string, DateTime> Visitors =
                            new System.Collections.Generic.Dictionary<string, DateTime>();
        Visitors.Add(Session.SessionID, DateTime.Now);        
        HttpContext.Current.Application.Lock();
        Application["Visitors"] = Visitors;
        HttpContext.Current.Application.UnLock();
    }

HttpHandleder.cs

private static void UpdateVisitors()
    {
        System.Collections.Generic.Dictionary<string, DateTime> Visitors = (System.Collections.Generic.Dictionary<string, DateTime>)HttpContext.Current.Application["Visitors"];
        Visitors[HttpContext.Current.Session.SessionID] = DateTime.Now;
    }

AnyPage.aspx.cs(オンラインユーザーの総数を取得する場所)

 protected int GetCurrentOnlineUsers()
        {
            int total = 0;
            Dictionary<string, DateTime> Visitors = (Dictionary<string, DateTime>)Application["Visitors"];
            foreach (KeyValuePair<string, DateTime> pair in Visitors)
            {
                TimeSpan Remaining = DateTime.Now - pair.Value;
                int remainingMinutes = Convert.ToInt32(Remaining.TotalMinutes);            
                if (remainingMinutes < 21) //Only count the visitors who have been active less than 20 minutes ago.
                    total++;
            }

            return total;
        }
于 2012-07-11T01:48:06.387 に答える
0

ハンスのソリューションは本当に素晴らしく、私が望むとおりに機能していますが、最近、MSDNでこの記事を見つけました。これは、この目的のベストプラクティスのようです。

参照:http ://msdn.microsoft.com/en-AU/library/system.web.httprequest.anonymousid.aspx

Global.asax

void Application_Start(Object sender, EventArgs e)
    {
        // Initialize user count property
        Application["UserCount"] = 0;
    }

public void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e)
    {
    // Change the anonymous id
    e.AnonymousID = "mysite.com_Anonymous_User_" + DateTime.Now.Ticks;

    // Increment count of unique anonymous users
    Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
}

.ASPXファイル

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  void Page_Load(object sender, EventArgs e)
    {
      if (Application["UserCount"] != null)
      {
          lblUserCount.Text = Application["UserCount"].ToString();
          lblCurrentUser.Text = Request.AnonymousID;
      }
  }    
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AnonymousID Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Number of users: 
        <asp:Label ID="lblUserCount" Runat="server"></asp:Label><br />
    Current user:
        <asp:Label ID="lblCurrentUser" Runat="server"></asp:Label><br />
    </div>
    </form>
</body>
</html>
于 2013-04-17T05:33:57.863 に答える
0

ユーザーIPをチェックしてユニーク訪問を取得してみませんか。それらをハッシュセットに追加して、ロケーションAPIを使用してヒートマップを作成するなどの優れた操作を実行できるようにします。

 // count users online on all desktopsites
        void Session_Start(object sender, EventArgs e)
        {
            try
            {
                // lock application object
                Application.Lock();

                // get hashset containing all online ip adresses
                var ips = (HashSet<string>)Application["visitors_online_list_ip"];

                // get user ip
                var ip = HttpContext.Current.Request.UserHostAddress;

                // add ip to hashset
                ips.Add(ip);

                // store ip in session to delete when session ends
                Session["ip"] = ip;

                // save hashset
                Application["visitors_online_list_ip"] = ips;

                // unlock application object 
                Application.UnLock();
            }
            catch {}
        }

        void Session_End(object sender, EventArgs e)
        {
            try
            {
                // lock application object
                Application.Lock();

                // get hashset containing all online ip adresses
                var ips = (HashSet<string>)Application["visitors_online_list_ip"];

                // get user ip from Session because httpcontext doesn't exist
                var ip = Session["ip"].ToString(); ;

                // remove ip from hashset
                ips.Remove(ip);

                // save hashset
                Application["visitors_online_list_ip"] = ips;

                // unlock application object 
                Application.UnLock();
            }
            catch {}
        }
于 2015-01-23T12:47:36.340 に答える