On 11th November, 2009 I have published the HitCounter webpart solution on to Codeplex
I have made the Hit Counter webpart very compact with minimum lines of code which generates user hits, before adding the webpart to the master page you need to create a Custom List name it as Statistics then create three columns in Statistics list as below:
- url Multiple lines of text
- date Single line of text
- uname Single line of text
Also, you have to give the Contribute – Can view, add, update, and delete permissions for the Visitor and Viewers for the Statistics list, so that visitor/viewers visits also recorded in the 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
- Finally click Ok button
Please Note: For applying Contribute – Can view, add, update, and delete permissions for Viewers repeat the above 1, 2, 3 and 4 steps
If a user is new to current page on the day, the code adds a record to Statistics list, if the same user visits the same page the code just returns out from condition and do nothing, because the user already visited the page on the same day, this is all because of to maintain the unique hits
To know the hit count for all pages in site, you have to place the webpart in master page, so that the webpart code runs on every page where the visitor go and makes the unique entry in Statistics list
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
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…?
@ 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)
@ 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 (:
@ 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!
@ 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
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.
@ 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 🙂
@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.
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
@ 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
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?
@ 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
Hi
Please help me ..
I not able to add Hit count webpart in sharepoint master page.
Error in rendering the webpart.
Thanks
Vardhan
@Vardhan: The message ‘Error in rendering the webpart’ appears in Sharepoint designer or in IE while try to access the site?
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
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.
@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
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
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
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?
For some reason I managed to miss “… and activate the feature HitCounter”
Now I have the functionality.
thank you.
@ Ulrich: Thanks for looking into the post, Good Luck 🙂
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.
@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
Indeed. But you should elaborate on this – maybe with a few screenshots in a installation doc on CodePlex. For future users ofcourse.
@Ulrich: Done! update the instructions on Codeplex, thanks for your suggestions.
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
@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.
Will this solution work with anonymous publishing sites (i.e. a MOSS public Internet site)?
@ 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
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!
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
@ 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
@ 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);
});
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
@ 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.
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
@ Soumya: You can pull the list data using CAML query from Statistics list and show the details (username, url, date) as you want
Example:
Thanks vijay. Thanks for yor help.
I have deployed the solution as per below and also created a Statistics List with columns advised.
stsadm -o addsolution -filename HitCounter.wsp
However, when I try to add Hitcounter webpart to site or page, getting following error !!. Any idea ?
Unable to add selected web part(s). The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML.
Hello Vijay,
i’m interested with your web part,
i have a need to create Hit Counter web Part, but it will be attached to a Button.
my needs :
in sharepoint page, i have a button.
if the button got clicked, it will be adding to a custom list :