how to count the number of times a visualforce page opened by user?

Google Analytics.

You could consider treating your VF pages, like any other HTML page and embed Google Analytics API into the page, the information capture is sent to your designated Google account, for you to review via their dashboards. Perhaps wrapping the use of the API in a Visualforce Component to ensure its easy to place on your pages by your developers.

Google Analytics provides easy to use APIs and SDKs to send data to Google Analytics. With the collection APIs and SDKs, you can measure how users interact with your content and marketing initiatives. Once implemented, you will be able to view user-interaction data within Google Analytics or through the Reporting APIs.

There are further examples of the JavaScript library Google provide, showing how to attach it to events on your page. There is also a setting to configure an opt-out mode depending on the privacy settings you provide in your app.


You can add an action method that will allow you to call a DML operation when the page loads. This works even on PDF rendered pages, etc. You can be as granular as you would like. I would recommend that you create an "extension" that would allow you an easy way to add the view counter to each page. The design could look like this:

public without sharing class ViewCount {
    public ViewCount(ApexPages.StandardController controller) { // Required with StandardController
    }

    public ViewCount(CustomController controller) { // Required for custom controller; repeat as necessary
    }

    public void updateCount() {
        Url thisUrl =   Url.getCurrentRequestUrl();
        String thisPath = thisUrl.getPath(), thisFile = thisUrl.getFile(), thisFullPath = thisPath + '/' + thisFile;
        Page_View__c[] views = [SELECT Id, URL__c, Count__c FROM Page_View__c WHERE URL__c = :thisFullPath LIMIT 1];
        Page_View__c thisView = views.isEmpty() ? new Page_View__c(Url__c=thisFullPath, Count__c=0) : views[0];
        thisView.Count__c++;
        upsert thisView;
    }

Then, on each page, simply add the action method:

<apex:page standardController="Account" extensions="ViewCount" action="{!updateCount}" ...

Some pages that already have an action may not be compatible this way, in which case you'll probably want to make updateCount an actionFunction that is called when the page is initially loaded:

<apex:actionFunction name="updateCount" action="{!updateCount}" reRender="" />
<script>
    addEventListener("load", updateCount, false);
</script>

You'll need a "form" tag if you don't already have one in order to call an actionFunction; you can wrap it around the actionFunction or around the entire contents of the page, whichever you prefer (but the former will produce a far smaller view state in most cases).

All objects mentioned here that do not exist will need to be created first. Users won't need permission to access the object, since it is updated "without sharing."


With Spring'14 Salesforce has announced a Custom Metrics Pilot which will provide this information.

In Spring '14, you can collect detailed usage metrics from each organization in which your managed package is installed. By analyzing this information, you can gain valuable insights into the utilization and performance of your app across your entire customer base.

You can collect the following daily metrics on two types of components in a managed package.

  • Custom objects — the total number of records stored per organization in each custom object. This enables you to track how the usage of that custom object is growing with time in any subscriber organization, which is a reliable indicator of how much it's being utilized
  • Visualforce pages — the number of times per organization each Visualforce page was accessed, the number of unique users who accessed it, and the average loading time (in milliseconds). By comparing the metrics for different Visualforce pages, you can determine the relative popularity of different parts of your app in a specific customer organization, as well as trends across all customers.