Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Anders Hattestad
Jan 7, 2011
  8372
(1 votes)

Using Statistics to show view count

In a current project I’m working on we needed a page view count. I first thought about making my own DDS table and add count values to that. But then I though about the Statistic tab in edit mode in EPiServer. What I wanted to do was to get the page count from that

image

Did some Google search about how this and did only find one forum post that pointed me in the right direction.

This code

Code Snippet
  1. DateTime start = DateTime.Now;
  2. TimeSpanQuery timeSpanQuery = new TimeSpanQuery();
  3. timeSpanQuery.Root = ((ITransformer)ClassFactory.CreateInstance(typeof(ITransformer), new object[0])).CreateURL(base.CurrentPage.PageLink.ID);
  4. timeSpanQuery.Interval = 5;
  5. timeSpanQuery.Stop = DateTime.Now;
  6. timeSpanQuery.Start = DateTime.Now.AddYears(-4);
  7. TimeSpanAnalyzerView view = new TimeSpanAnalyzerView();
  8. Uri address = new Uri("soap.tcp://localhost/TimeSpanAnalyzerView");
  9. Uri via = new Uri(VirtualPathUtility.AppendTrailingSlash(Settings.Instance.LogServiceUrl.ToString()) + "TimeSpanAnalyzerView");
  10. view.Destination = new EndpointReference(address, via);
  11. var data = view.GetHits(timeSpanQuery);

results in these 3 tables

image

and its the first part of the statistic tab information

And this code

Code Snippet
  1. RealTimeAnalyzerView view = new RealTimeAnalyzerView();
  2. Uri address = new Uri("soap.tcp://localhost/RealTimeAnalyzerView");
  3. Uri via = new Uri(VirtualPathUtility.AppendTrailingSlash(Settings.Instance.LogServiceUrl.ToString()) + "RealTimeAnalyzerView");
  4. view.Destination = new EndpointReference(address, via);
  5. ITransformer transformer = (ITransformer)ClassFactory.CreateInstance(typeof(ITransformer), new object[0]);
  6. DataSet data=view.Referrers(transformer.CreateURL(base.CurrentPage.PageLink.ID));

results in

image

that is the last part. There are other information that can be retrieved.

 

  1. data = view.LatestUsers(transformer.CreateURL(base.CurrentPage.PageLink.ID));

results in

image

and the

Code Snippet
  1. data = view.GetMaxHits(transformer.CreateURL(base.CurrentPage.PageLink.ID));

image

Here you will default get max 5 elements if you not change EPnMostVissitPagesMaxCount in the C:\Program Files (x86)\EPiServer\Shared\Services\Log Service\EPiServer.LogService.exe.config

Code Snippet
  1. <sendListener type="EPiServer.Log.Analyzer.RealTimeAnalyzer, EPiServer.Log.Analyzers" method="StoreMessage">
  2.     <!-- The view for the analyzer -->
  3.     <view type="EPiServer.Log.Analyzer.RealTimeAnalyzerView, EPiServer.Log.Analyzers" protocol="TCP/SOAP" endpoint="soap.tcp://localhost/RealTimeAnalyzerView"/>
  4.     <!-- Maximum pages in the published pages queue -->
  5.     <EPnPublishedPagesMaxCount>5</EPnPublishedPagesMaxCount>
  6.     <EPnMostVissitPagesMaxCount>100</EPnMostVissitPagesMaxCount>
  7. </sendListener>

It can seems that the TimeSpanAnalyzerView methods persists and that the RealTimeAnalyzerView is cleared when the server resets. So if you want to have a count of how many times users have accessed on page this is the code that will return that

Code Snippet
  1. TimeSpanQuery timeSpanQuery = new TimeSpanQuery();
  2. timeSpanQuery.Root = ((ITransformer)ClassFactory.CreateInstance(typeof(ITransformer), new object[0])).CreateURL(base.CurrentPage.PageLink.ID);
  3. timeSpanQuery.Interval = 5;
  4. timeSpanQuery.Stop = DateTime.Now;
  5. timeSpanQuery.Start = DateTime.Now.AddYears(-4);
  6. TimeSpanAnalyzerView view = new TimeSpanAnalyzerView();
  7. Uri address = new Uri("soap.tcp://localhost/TimeSpanAnalyzerView");
  8. Uri via = new Uri(VirtualPathUtility.AppendTrailingSlash(Settings.Instance.LogServiceUrl.ToString()) + "TimeSpanAnalyzerView");
  9. view.Destination = new EndpointReference(address, via);
  10. var data = view.GetHits(timeSpanQuery);
  11. string nrofVisits = "";
  12. if (data != null && data.Tables["Total"] != null && data.Tables["Total"].Rows.Count > 0)
  13.     nrofVisits = "" + data.Tables["Total"].Rows[0][0];
  14. if (nrofVisits != "")
  15.     NrOfUnikVisits.Text = string.Format(NrOfUnikVisits.Text, nrofVisits);
Code Snippet
  1. <asp:Literal ID="NrOfUnikVisits" runat="server" Text="{0} visninger" EnableViewState="false" />

image

Jan 07, 2011

Comments

Magnus Rahl
Magnus Rahl Jan 11, 2011 10:45 AM

I used this for ranking the top visited pages as you might have gathered from the forum thread. However, apparently this functionality is being deprecated in a future version of EPiServer, or have I been misinformed?

Please login to comment.
Latest blogs
Further Enhancing Production Database Scalability in DXP Cloud Services

About a year ago we announced that we are improving the scalability of SQL databases in DXP Cloud Services , focusing first on non-production...

Anna Pleshakova | Jan 27, 2025

Automatic Alt-Text for Images with AI Assistant for Optimizely

Creating accessible and user-friendly content has never been more critical. For websites, ensuring that images include descriptive alt-text is an...

Luc Gosso (MVP) | Jan 26, 2025 | Syndicated blog

Optimizely CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog

Decimal numbers in Optimizely Graph

Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as...

Damian Smutek | Jan 23, 2025 | Syndicated blog

Find and delete non used media and blocks

On my new quest to play around with Blazor and MudBlazor I'm going back memory lane and porting some previously plugins. So this time up is my plug...

Per Nergård (MVP) | Jan 21, 2025

Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025