Visitor
Unique Visitor Id
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
Your Unique Visitor ID
Visit Histories
Visitor ID | Visitor Date |
---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; namespace WebApiExplorer.Controllers.Visitor { public partial class VisitorController : Controller { public IActionResult UniqueVisitorId() { 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | @using Microsoft.Extensions.Configuration @inject IConfiguration Configuration @ { ViewBag.DemoDescription = false ; } @section Styles{ < style > #map { height: 450px; width: 100%; } #histories-container { max-height: 249px; overflow-y: auto; } </ style > } < h3 > @Html .Raw(Visitor.Txt_Title_Unique_VisitorId) </ h3 > @section Summary{ @Html .Raw(Visitor.Description) } < div > < div class = "col-md-6" > < table class = "table" > < tbody id = "current" > < tr > < td id = "visits" >< span id = "visitorId" ></ span ></ td > </ tr > < tr > < td id = "timer" ></ td > </ tr > < tr > < td id = "totalVisitedTime" ></ td > </ tr > </ tbody > </ table > < div class = "h4" > @Html .Raw(Visitor.Txt_Title_Visitor_Visit_Histories)</ div > < div id = "histories-container" > < table class = "table" > < thead > < tr > < th > @Html .Raw(Visitor.Txt_Title_VisitorId)</ th > < th > @Html .Raw(Visitor.Txt_Visit_Date)</ th > </ tr > </ thead > < tbody id = "histories" > </ tbody > </ table > </ div > </ div > < div class = "col-md-6" > < div id = "map" ></ div > </ div > </ div > <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> <script async defer src="https://maps.googleapis.com/maps/api/js?key=@(Configuration["GoogleMapApiKey"])"> </script> <script> window.onload = function () { 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; }); }; } window.visitor.getDataAsync(handleGetVisitorDataAsync); function createMap(geo) { var position = { lat: geo.latitude, lng: geo.longitude }; var map = new google.maps.Map(document.getElementById( 'map' ), { zoom: 14, center: position }); var marker = new google.maps.Marker({ position: position, map: map }); } function handleGetVisitorDataAsync(data) { startTimer(); saveVisitorData(data.visitorId, data.session); var visitorIdElement = document.getElementById( 'visitorId' ); visitorIdElement.innerHTML = data.visitorId; createMap(data.geo); bindHistories(data.visitorId); } function setTotalVisitedTime(time) { document.getElementById( 'totalVisitedTime' ).innerHTML = '@Html.Raw(Visitor.Template_Total_Visits_Time)' .format(time); } function saveVisitorData(visitorId, session) { var key = genKey(visitorId); var pureItem = { visitorId: visitorId, start: session.start }; localStorage.setItem(key, JSON.stringify(pureItem)); function genKey(visitorId) { return 'visitorId-' + visitorId + '-' + new Date().toISOString(); } } function bindHistories(visitorId) { var source = []; for ( var key in localStorage) { if (matched(key)) { source.push(JSON.parse(localStorage.getItem(key))); } } renderHistories(); setTotalVisitedTime(source.length); function renderHistories() { var table = document.getElementById( "histories" ); var sortedSource = source.sort( function (a, b) { a = new Date(a.start); b = new Date(b.start); return a > b ? -1 : a < b ? 1 : 0; }); for ( var i = 0 ; i < sortedSource.length; i++) { renderRow(i, sortedSource[i]); } function renderRow(index, visitorHistory) { var row = table .insertRow(index); renderCell(0, visitorHistory.visitorId); renderCell(1, visitorHistory.start); function renderCell(cellIndex, text) { var cell = row .insertCell(cellIndex); cell.innerHTML = text ; } } } function matched(key) { return key.indexOf( 'visitorId-' + visitorId) > -1; } } var browsingTimeTemplate = '@Html.Raw(Visitor.Template_Browsing_Time)' ; function startTimer() { var now = 1; setTimerLabel(); window.setInterval( function () { now++; setTimerLabel(); }, 1000); function setTimerLabel() { document.getElementById( 'timer' ).innerHTML = browsingTimeTemplate + ' ' + now + '(s)' ; } } } </script> |