function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Code+1Code+1 

Identify number of times a Visualforce page has been opened

Hello All,
 
 I would like to get a sample code to identify "How many times a Visual force page has been opened / hit " by the user.
 Please guide me with the code (Visualforce remoting way / apex:variable way / best possible solution) which we can propose to customer.
 
Thanks !

 
Shrikant BagalShrikant Bagal
Hello krishna,

You can easily create your own tracking mechanism. Create an object, let's say VF_Tracking__c, that has a lookup(User) User__c, text(255) VF_Page_Visited__c, and dateTime Datetime_Visited__cplus anything else you want to track.

In your Visualforce controller, create a pageReference method to track who visits the page, and use it in the Action parameter in the apex:page compenent in your VF page.

Example:

Controller code:
public class myVFController{

    public pageReference trackVisits(){
        VF_Tracking__c newTracking = new VF_Tracking__c(User__c=UserInfo.getUserId(),VF_Page_Visited__c='myVFPage',Datetime_Visited__c=datetime.now());
        insert newTracking;
        return null;
    }//END trackVisits

}//END class

VF:
 
<apex:page action="{!trackVisits}">

</apex:page>

FYI: You can do it using RemoteAction to increase the performance

Hope its help you.
viruSviruS
Please update Vf Page by 


<apex:page action="{!trackVisits}" controller="myVFController"> </apex:page>
 
Code+1Code+1
Hello Shrikant / Virus,
 Can you please share thoughts by using Visual force remoting way.
 I need to update the counter in VF Page by "1" when there is an additional hit to the Visualforce Page.
 So, that i can track the total number of visits to the site page.


Thanks !
 
viruSviruS
Hi KK,

Very simple and easy example  https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_example.htm
Code+1Code+1
Hi Virus,
 The example shared fetches the account through Javascript remoting and displays in the page.
 
 I need to update the counter in VF Page by "1" when there is an additional hit to the Visualforce Page.
 So, that i can track the total number of visits to the site page.

Thanks !