ComponentOne
Web API Explorer ASP.NET Web API Explorer

Visitor

Available Variables

The .NET Core Visitor Web API collects user data such as IP, Geographical location, language, referring site, session, operating system, device, browser. It's useful for web developers to customize content for individual users

Features

Available Variables

Visitor ID
IP Address
Language/Locale
Web Browser
Operating System
(Mobile) Device
Geographical Location
Current Session
First/Initial Session

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
  
namespace WebApiExplorer.Controllers.Visitor
{
  public partial class VisitorController : Controller
  {
    Dictionary<string, string> VariablesToDisplay = new Dictionary<string, string>()
    {
      { "visitorId", Localization.Visitor.Txt_Title_VisitorId},
      { "ip", Localization.Visitor.Txt_Title_Ip},
      { "locale", Localization.Visitor.Txt_Title_Locale},
      { "browser", Localization.Visitor.Txt_Title_Browser},
      { "os", Localization.Visitor.Txt_Title_Os},
      { "device", Localization.Visitor.Txt_Title_Device},
      { "geo", Localization.Visitor.Txt_Title_Geo},
      { "session", Localization.Visitor.Txt_Title_Session},
      { "firstSession", Localization.Visitor.Txt_Title_First_Session}
    };
  
    public IActionResult AvailableVariables()
    {
      ViewBag.VariablesToDisplay = this.VariablesToDisplay;
      return View();
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
  
    <script>
        function loadedContent() {
            c1.webapi.VisitorConfig.API_HOST.url = "https://demos.componentone.com/ASPNET/5/C1WebAPI/3.0.20202.265";
        }
    </script>
<script src="@(Configuration["WebAPIService"])api/visitor/visitor-client.min.js" onload="loadedContent();"></script>
@{
  ViewBag.DemoDescription = false;
  var VariablesToDisplay = @Html.Raw(Json.Serialize(ViewBag.VariablesToDisplay));
}
@section Styles{
  <style>
    .visualize-container {
      line-height: 30px;
    }
  
    .visualize-container {
      display: flex;
      flex-flow: row wrap;
    }
  
    .grid-col {
      flex: 1 0 25%;
      max-width: 25%;
      overflow-wrap: break-word;
    }
  
    .danger {
      color: red;
    }
  
    .success {
      color: #00c1d5;
    }
  
    @@media only screen and (max-width: 1000px) {
      .grid-col {
        flex-basis: 33.33%;
        max-width: 33.33%;
      }
    }
  
    @@media only screen and (max-width: 768px) {
      .grid-col {
        flex-basis: 50%;
        max-width: 50%;
      }
    }
  
    @@media only screen and (max-width: 600px) {
      .grid-col {
        flex-basis: 100%;
        max-width: 100%;
      }
    }
  </style>
}
  
<h3>
  @Html.Raw(Visitor.Txt_Variables)
</h3>
  
<div class="visualize-container">
  @foreach (var res in ViewBag.VariablesToDisplay)
  {
    <div class="grid-col">
      <div>
        <strong>@res.Value</strong>
      </div>
      <div id="id-info-@res.Key"></div>
    </div>
  }
</div>
  
<p id="license-info"></p>
  
@section Summary{
  @Html.Raw(Visitor.Description)
}
  
  
<script>
  if (!String.prototype.format) {
    String.prototype.format = function () {
      var args = arguments;
      return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined'
          ? args[number]
          : match
          ;
      });
    };
  }
  
  var templates = {
    undefined: "<span class=\"danger\"> null </span>",
    value: "<span class=\"success\"> {0}</span>",
    date: "<span class=\"success\"> {0}</span>",
    object_content: "<div style=\"padding-left:{0}px\"> {1}: {2}</div>",
    object: "<ul>{0}</ul>",
    na: "<span> n / a </span>",
  }
  
  function isISODateString(str) {
    return /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/.test(str);
  }
  
  function visualizeObject(obj, deep) {
    deep = deep === undefined ? 0 : deep + 1;
    if (obj == null) return templates.undefined;
    if (typeof obj == 'boolean') return templates.value.format(obj ? "true" : "false");
    if (obj instanceof Date) return templates.value.format(obj.toLocaleString());
    if (typeof obj == 'object') {
      const keys = Object.keys(obj);
      var content = "";
      for (var i = 0; i < keys.length; i++) {
        content += templates.object_content.format(deep > 0 ? 30 : 0, keys[i], visualizeObject(obj[keys[i]], deep));
      }
      return templates.object.format(content);
    }
    if (typeof obj == 'string' && isISODateString(obj)) return templates.value.format(new Date(obj).toLocaleString());
    if (obj) return templates.value.format(obj);
    else return templates.na;
  }
  
  function render(visitor) {
    var variables = @VariablesToDisplay;
    var keys = Object.keys(variables);
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      var ele = document.getElementById("id-info-" + key);
      if (ele) {
        ele.innerHTML = visualizeObject(visitor[key]);
      }
    }
    renderIp2LocationLicensing();
  }
  
  function isIp2Location() {
    if (!c1.webapi.VisitorConfig || !c1.webapi.VisitorConfig.SERVER_DATA) return false;
    var visitorData = c1.webapi.VisitorConfig.SERVER_DATA;
    if (visitorData) {
      return visitorData.locationProviderType == 1;
    }
    return false;
  }
  
  function renderIp2LocationLicensing() {
    if (!isIp2Location()) return;
    var ele = document.getElementById("license-info");
    if (ele) {
      ele.innerHTML = "<small>This product includes IP2Location LITE data available from <a href=\"https://www.ip2location.com\">https://www.ip2location.com</a> </small>";
    }
  }
  
  window.visitor.getDataAsync(function (data) {
    render(data);
  });
  
</script>