Sharepoint hit counter web part

15 September, 2009 (11:26) | MOSS - Object Model | By: G Vijai Kumar

On 11th November, 2009 I have published the HitCounter webpart solution on to Codeplex

You can download the Hit Counter webpart from http://hitcounter.codeplex.com/

Comments on this solution are very much appreciated :-)

Thanks for looking into this

Comments

Comment from Kristopher
Time September 23, 2009 at 3:02 pm

What type of project did you create in Visual Studio? I initially assumed a Class Library but now am wondering if I should create it as a SharePoint Web Part…?

Comment from G Vijai Kumar
Time September 24, 2009 at 6:09 am

@ Kristopher: I choose webpart template and created the hit counter webpart, even I suggest the same, although there won’t be harm if you choose class library but you have to put additional effort in adding the reference (Microsoft.Sharepoint.dll)

Comment from Kristopher
Time September 24, 2009 at 2:13 pm

@ Admin: Thank you! I built this yesterday with your code using the Web Part project. This is a perfectly functional application immediately after deploying. I am building in functionality to check to see if a site already has the list created. If not, my code will create the list, create the columns, and set the permissions to Contribute when the web part is added to a page. Also, I removed the portion of the query that checks for only unique hits per day because the customer wanted it to behave like a FrontPage-style hit counter. Beautiful code and thank you for your service to the community (:

Comment from Charles
Time September 24, 2009 at 6:47 pm

@ admin
I am not a developer not do I have access to developer tools to even attempt making an application, I am more of a site admin with limited controls. Please forgive me if my question sounds technically naïve.
Would it be possible to use this web part to count the number of hits on anything, (documents, pages, wiki pages, ect ect) and save it within a list?
I would like to automatically count hits on items in libraries, and or lists. Is this possible?
Could you also recommend a good sharepoint users forum?

Thank you for your article!

Comment from G Vijai Kumar
Time September 25, 2009 at 5:27 am

@ Charles: The hit counter webpart records the user visits on pages, basically it works on Sharepoint pages whether it can be content pages, list/library pages etc. If you want to know hit counts on items in list/library please look into the thread http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c66c1b27-8d2b-48ca-a1d8-e36565e27468
I would prefer Microsoft Technet forum is the best place for Sharepoint users, here you go http://social.technet.microsoft.com/Forums/en-US/category/sharepoint

Comment from Jayashri
Time September 28, 2009 at 12:24 pm

Hi,

First Of All Thanks for this code.
I tried it at my side, it works.But works only when this webpart gets added into a page.
Is it necessary to insert this webpart on all pages?
cant we have option such as if we copy DLL of this into bin, it should automatically fetch all pages hits of this site into a list?

Thanks in advance.

Comment from G Vijai Kumar
Time September 29, 2009 at 5:11 am

@ Jayashri, Thanks for looking into the post. No need of placing the webpart on all pages, as I mentioned already in the post that, to know the hit counts for all pages in a site, you have to place the webpart in master page (default.master) instead of content pages (default.aspx)
so that the webpart code runs on every page and makes the unique entry in ‘Statistics’ list, use Sharepoint designer to place hit counter webpart in master page, Good Luck :-)

Comment from Kristopher
Time September 29, 2009 at 11:29 am

@Jayashri: Good luck, indeed! I was not able to place a webpart on the Master Page using the GUI. Neither could I from SharePoint Designer.

Comment from Mike Lydiat
Time October 16, 2009 at 2:14 pm

As a total newbie to coding, I am looking for a hit / visitor counter .dwp file I can import to a SharePoint page without using Visual Basic or SharePoint Designer. Does one exist anywhere? I have Googled for days now unsuccessfully.

Mike

Comment from G Vijai Kumar
Time October 16, 2009 at 6:18 pm

@ Mike Lydiat: Send me your e-mail details through contact form on page http://www.fivenumber.com/contact/ so that I can send you the .wsp file by which you can easily install through single command

Comment from P LYNCH
Time October 26, 2009 at 9:45 am

Seems to be a few noobs like Mike and myself looking for a counter. I am a site admin and can ‘pseudo code’ in css and manipulate master pages in SPD. But cannot program in VS. I also do not have access to the 12 hive to implment a .wsp file. I’m trying to find out how to add a counter to my site in a webpart and your example look great, better than the web component counter in SPD. I wonder if I could create a sharepoint custom list like you (add columns above), and add the code above as a script in a CEWP, would this work for the one page I need this?

Or could I get a copy of the dwp file and edit the webpart code in notepad to work with my site?

Comment from G Vijai Kumar
Time October 26, 2009 at 6:59 pm

@ PLYNCH: Script doesn’t show the all time unique hits, there should be some code behind to insert items into list and calculate the item count of a particular user, you can use the third party utilities like http://www.statcounter.com script and place it in master page using SPD

Comment from Harsha vardhan
Time December 4, 2009 at 5:54 am

Hi
Please help me ..
I not able to add Hit count webpart in sharepoint master page.
Error in rendering the webpart.

Thanks
Vardhan

Comment from G Vijai Kumar
Time December 4, 2009 at 5:59 am

@Vardhan: The message ‘Error in rendering the webpart’ appears in Sharepoint designer or in IE while try to access the site?

Comment from Harsha Vardhan
Time December 4, 2009 at 12:35 pm

Hi Vijay

The message ‘Error in rendering the webpart’ shows when I try to add webpart in Sharepoint Designer.Could you please help me where i am going wrong.

Thanks
Vardhan

Comment from Harsha Vardhan
Time December 4, 2009 at 3:03 pm

The message ‘Error in rendering the webpart’ shows when I try to add webpart in Sharepoint Designer.Could you please help me where i am going wrong.

Comment from G Vijai Kumar
Time December 4, 2009 at 3:48 pm

@Vardhan: Have you created ‘Statistics’ list with columns as below:
1. url – Multiple lines of text
2. date – Single line of text
3. uname – Single line of text

Please confirm

Comment from Harsha vardhan
Time December 5, 2009 at 7:04 am

Hi vijay

Yes, I created statistics list with columns as given.
I downloaded wsp and deployed in site. I am able to get the Hitcounter webpart in default.aspx. The urls aslo enter the statistics list correctly.
After that I open the sharepoint designer and tried to add the Hit counter webpart to Master page. It is showing “Error in Rendering the Webpart object reference not set”. Even I added the tagprefix to master page aslo.

Could you Please Help me.

Thanks,
Vardhan

Comment from Harsha vardhan
Time December 5, 2009 at 7:11 am

Hi vijay

I tried Your Code Solution and deployed in Site. It works fine in default.aspx. when I tried to add the same Hit counter webpart in master page it also showing “Error in render webpart”.

Please help me as it was high priority requriement to me.

Thanks,
Vardhan

Pingback from Restrict SharePoint Designer usage as needed | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 5:23 am

[...] Sharepoint hit counter web part Share [Translate] [...]

Pingback from Create Powerful Reusable Workflows | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 5:31 am

[...] Sharepoint hit counter web part Share [Translate] [...]

Pingback from Create External Content Types using Business Connectivity Services | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 11:30 am

[...] Sharepoint hit counter web part [...]

Pingback from Connect to Data Sources outside of SharePoint | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 12:18 pm

[...] Sharepoint hit counter web part [...]

Pingback from Use XSLT List View web parts to show dynamic views of your data | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 12:21 pm

[...] Sharepoint hit counter web part [...]

Pingback from Create Site Assets for your site | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 12:26 pm

[...] Sharepoint hit counter web part [...]

Pingback from Create Content Types and attach to Lists directly | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 12:27 pm

[...] Sharepoint hit counter web part [...]

Pingback from Configure Site Security | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 12:29 pm

[...] Sharepoint hit counter web part [...]

Pingback from New User Experience with Summary Pages, Ribbon and Quick Launch navigation | PortalArabic – Arabic Shared Data
Time December 6, 2009 at 12:31 pm

[...] Sharepoint hit counter web part [...]

Comment from Ulrich Bernskov
Time January 14, 2010 at 2:18 pm

Hi Vijay
Thank you for sharing your component.
I am missing some kind of step to implement the functionality on a site. I have created a list with the 3 coloumns.
I have used the STSADM on the server and I can go in the backend and publish the solution to the webfrontend with success. I have also done an additional IISRESET just to be sure.

Then I assumed that I could see a new webpart in the GUI after using “Site Action” “Edit page” and then add webpart.
But I have a suspicion that I am missing something?
Should I edit the ‘default.aspx’ in SPD and if so – what should I do?

Comment from Ulrich Bernskov
Time January 14, 2010 at 2:29 pm

For some reason I managed to miss “… and activate the feature HitCounter”
Now I have the functionality.
thank you.

Comment from G Vijai Kumar
Time January 14, 2010 at 3:33 pm

@ Ulrich: Thanks for looking into the post, Good Luck :-)

Comment from Ulrich Bernskov
Time January 15, 2010 at 7:52 am

Well – next issue is the rights that needs to be on the “statistics” custom list. Maybe you should elaborate on this. I just got an access denied for ordinary users or visitors, because they cannot add (contribute) to the list.

Comment from G Vijai Kumar
Time January 15, 2010 at 9:03 am

@Ulrich: You have to give the “Contribute – Can view, add, update, and delete” permissions for the visitor for the ‘Statistics’ list, so that visitor visits also recorded in that particular list.
To give permissions: Go to Statistics list, Settings > List Settings, under ‘Permissions and Management ‘ click on ‘Permissions for this list’, select ‘Visitors ‘ checkbox then click on ‘Actions’ > ‘Edit User Permissions’ then select ‘Contribute – Can view, add, update, and delete’ click Ok button

Comment from Ulrich Bernskov
Time January 15, 2010 at 9:16 am

Indeed. But you should elaborate on this – maybe with a few screenshots in a installation doc on CodePlex. For future users ofcourse.

Comment from G Vijai Kumar
Time January 15, 2010 at 2:48 pm

@Ulrich: Done! update the instructions on Codeplex, thanks for your suggestions.

Comment from Ulrich Bernskov
Time January 15, 2010 at 3:02 pm

Excelent. The next request/wish would be some kind of trim to display the friendly name of the user. I get the hits in the DOMAIN\user format and would prefer something like “Firstname Lastname”

Another thing is that it appears that you use hardcoded date format. Not the system setting. In my case that gives MM/DD/YYYY as in your screencapture on Codeplex and we use DD/MM/YYYY

Comment from G Vijai Kumar
Time January 15, 2010 at 3:11 pm

@Ulrich: I have used Indian Date format (DD/MM/YYYY) for sure will definitely update the said suggestions in the next release of Hit Counter webpart, thank you.

Comment from Ryan Mendel
Time February 1, 2010 at 9:04 pm

Will this solution work with anonymous publishing sites (i.e. a MOSS public Internet site)?

Comment from G Vijai Kumar
Time February 2, 2010 at 5:49 am

@ Rayn Mendel: This solution won’t work for anonymous users, but I promise that, I will release a next version which works for anonymous users too

Comment from Chatfield
Time February 16, 2010 at 8:06 pm

The webpart will not even allow an anoymous user to laod the page, would be nice if a person could atleast allow the page to load- albeit not count the anonymous hit. Any way to change that for the time being?

Looking forward to counter working for ALL hits- anonymous included. Thanks!

Comment from Soumya
Time March 3, 2010 at 1:40 pm

Hi Vijay,

Can you please let me know how to create a webpart which track the number of users on a site prior to and after the current month. like username ,url visited,date,etc..

Thanks in advance,
Soumya

Comment from G Vijai Kumar
Time March 3, 2010 at 1:50 pm

@ Soumya: Hit Counter webpart available at Codeplex will meet your requirement, it shows user name, URL visited, date, total no.of hits etc, let me know in case if you need more details, thanks for looking into the post

Comment from G Vijai Kumar
Time March 3, 2010 at 2:04 pm

@ Chatfield: I have a thought to update the Hit Counter webpart which works for anonymous users too, but don’t know when I can release, for time being you can elevate the privileges for anonymous users and no hit counts for those users
try to incorporate the below lines of code in Hit Counter webpart solutions
//Elevate Privileges
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite mySite = SPControl.GetContextSite(Context);
});

Comment from Soumya
Time March 4, 2010 at 3:57 pm

Hi Vijay,

Thanks for your reply. Even I have tried the hitcounter web part available on codeplex . But there the details are storing in a list(named statistics). I have created a view to dispalythe details. I am getting username,date and the total count correctly.Only the Url is not getiing it properly.

The sitecollections and site visited, the url is always storing with (/Forms/AllItems.aspx) only.

Is there a way I can get

1. original site url visited (the exact url not the library url)

2. any html pages visited (which is stored inside the list)

Please help me and provide more details since I am new to sharepoint development :(

Thanks again for your help

Soumya

Comment from G Vijai Kumar
Time March 4, 2010 at 4:23 pm

@ Soumya: I guess you have placed the Hit Counter webpart in /Form/AllItems.aspx page (correct me if I am wrong) that’s why the counter is always adding the page (/Form/AllItems.aspx) in ‘Statistics’ lists.
You have to place the Hit Counter webpart in master page instead of content pages, so that the counter records the hits for all the site pages.
Note: Hit Counter webpart won’t count the hits of HTML pages, for that you have to choose some third party scripts or services.

Comment from Soumya
Time March 4, 2010 at 5:37 pm

You are correct vijay …

If I place the hit counter web part in master page it will show the count correctly (only I will get the count). But if I want to show the details (username, url, date visited), I need to create a view for the list. (Because it’s storing all the information in the list ["Statistics"] only.)

How can I get the information also in the same web part.

Just to make sure we both are in the same page; provided the URL I have followed http://hitcounter.codeplex.com/ . If not please provide me the correct URL.

Very much appreciated your help.

Thanks,
Soumya

Comment from G Vijai Kumar
Time March 5, 2010 at 5:56 am

@ Soumya: You can pull the list data using CAML query from Statistics list and show the details (username, url, date) as you want
Example:

                SPSite mysite = new SPSite("http://");
                SPWeb myweb = mysite.OpenWeb();
                SPList mylist = myweb.Lists["Statistics"];
                SPQuery myquery = new SPQuery();
                myquery.Query = "";//your query here.....
                SPListItemCollection myitemcol = mylist.GetItems(myquery);                

                foreach (SPListItem li in myitemcol)
                {
                    string url = li["url"].ToString();
                    string date = li["date"].ToString();
                    string username=li["uname"].ToString();
		}

Comment from Soumya
Time March 8, 2010 at 10:01 am

Thanks vijay. Thanks for yor help.

Write a comment