Is this really the version you are looking for? Kentico Xperience 11 Documentation is no longer updated. Use the switcher in the header to choose your product version.
The system allows you to track custom events as web analytics and display the results in reports. The following example logs a basic button click event, but you can use the same approach to implement tracking for any type of event on your website.
This example logs the event through a user control:
Open your Kentico web project in Visual Studio.
Create a New folder under the root named according to the code name of your site, e.g. CorporateSite (if it doesn't already exist).
Create a Web User Control in the new folder named AnalyticsButton.ascx.
Add the following code to the control:
<asp:Button id="btnLog" runat="server" Text="Add analytics hit" onclick="btnLog_Click" />The code inserts a standard Button control, which will be used to add hits to the custom statistic.
Switch to the code behind file of the control and modify it according to the following:
Note: The name of the class will be different according to the code name of your site.
using CMS.Membership;
using CMS.SiteProvider;
using CMS.WebAnalytics;
public partial class CorporateSite_AnalyticsButton : System.Web.UI.UserControl
{
// Handler for the button's click event
protected void btnLog_Click(object sender, EventArgs e)
{
// Gets the name of the current site
string siteName = SiteContext.CurrentSiteName;
// Checks if web analytics are enabled in the settings
if (AnalyticsHelper.AnalyticsEnabled(siteName))
{
// Adds a hit to a custom statistic
HitLogProvider.LogHit("buttonclicked", siteName, null, MembershipContext.AuthenticatedUser.UserName, 0);
}
}
}You can log custom events for web analytics via the API using the HitLogProvider class from the CMS.WebAnalytics namespace, specifically the following method:
LogHit(string codeName, string siteName, string culture, string objectName, int objectId, int count, double value)
Save the user control's files.
When a user clicks the button, the API creates an analytics log. Once the system processes the log, the results containing the given user's name and the amount of clicks are stored in the database, where they can be used by the web analytics application.
Note
Custom analytics do not automatically use JavaScript logging. If your website has JavaScript logging enabled, you may need to leverage JavaScript in the code to keep your custom statistics consistent with the default web analytics.
Now you need to publish the user control on your website (this example uses the sample Corporate site).
There are also other ways to add custom web analytics functionality to the pages of your website. For example:
Using the Analytics custom statistics web part
If you wish to log a simple event when visitors access a specific page, you can do so without having to write any code using the Analytics custom statistics web part.
Once this web part is placed on a page, it automatically adds a hit to a custom statistic when the given page is viewed. You can specify the details of the statistic, such as its code name, the name of the related object or the hit value using the web part's properties. The web part automatically loads the site name and culture from the context of the page.
Before users can view the statistics of the button click event in the Web analytics application, you need to create reports for displaying the data of the custom statistic. Most statistics have five types of reports: hourly, daily, weekly, monthly and yearly.
Open the Reporting application and select the Web Analytics category in the tree.
Click ... next to the New report button above the tree and select New category in the menu.
Type Button clicks as the Category display name and click Save.
Create a daily report for the new statistic. Click New report and enter the following values:
Report display name: Button clicked - daily report
Report name: buttonclicked.dayreport
The names of the reports must use a specific format:
Click Save.
Switch to the Parameters tab of the new report and use the New field button to create three essential parameters, which the web analytics use internally:
Field name: FromDate
Data type: Date and Time
Display field in the editing form: disabled
Click Save for every parameter before moving on to the next one.
Field name: ToDate
Data type: Date and Time
Display field in the editing form: disabled
The two parameters above define a time interval used to limit which data of the statistic should be loaded. Only hits that occurred during the specified interval will be displayed in the report. The values of these parameters can be set by users when viewing the data of the statistic in the web analytics interface.
The system uses the CodeName parameter to identify from which statistic the report loads data. The value is usually not modified if the report is dedicated to a single statistic (like the one in this example).
Go to the General tab and click New in the Tables section below the layout editor. Fill in the table's properties as shown below:
Display name: Table
Code name: Table_ButtonClicked_Day
Enable export: true (checked)
Query:
SET @FromDate = dbo.Func_Analytics_DateTrim(@FromDate,'day');
SET @ToDate = dbo.Func_Analytics_EndDateTrim(@ToDate,'day');
SELECT StatisticsObjectName as 'User', SUM(HitsCount) as 'Hits'
FROM Analytics_Statistics, Analytics_DayHits
WHERE (StatisticsSiteID = @CMSContextCurrentSiteID)
AND (StatisticsCode = @CodeName)
AND (StatisticsID = HitsStatisticsID)
AND (HitsStartTime >= @FromDate)
AND (HitsEndTime <= @ToDate)
GROUP BY StatisticsObjectName
ORDER BY SUM(HitsCount) DESCClick Save & Close to create the table.
Click Insert in the Tables section to add the new table into the report.
Click Save.
The daily report is now complete. In a real‑world scenario, the report would usually also contain a graph to display the data. See the Working with system reports chapter to learn more about graphs and other reporting options.
You can create the reports for the remaining time intervals using the same approach. The only differences should be in the names of the reports and tables, and the code of the queries, where you need to load data from the appropriate tables – Analytics_YearHits in the yearly report, Analytics_MonthHits in the monthly report, etc. You can make this process easier by using the Clone report () button in the menu above the report tree. Simply create copies of the daily report and then make the necessary adjustments.
To ensure that the user interface displays an appropriate title for your custom statistic, you need to create a resource string:
The system uses the string as the title for the matching statistic.
Once there are some hits logged in the database for a custom statistic, the system automatically displays the statistic under the Custom category of the tree in the Web analytics application. The reports of the sample statistic show the number of hits logged for the tracked button click event.
The new report is a daily report, so the data of the table is retrieved from the Analytics_DayHits table, together with the Analytics_Statistics table. See Web analytics API for more information about the database table structure.
Trimming the values of the FromDate and ToDate parameters
The SET statements included in the query are necessary to ensure that the specified time interval includes the first and last units of time (e.g. the exact days entered into the From and To fields of a daily report).
The second parameter of the trimming functions must match the time unit of the given report. The options are: 'hour', 'day', 'week', 'month', 'year'.