Setup Sitefinity CMS Analytics with Tracking consent

IMPORTANT: Sitefinity CMS Analytics module is unavailable after July 1, 2024, because it depends on the discontinued Google Universal Analytics. We recommend using Sitefinity Insight and the Script manager (available in Sitefinity CMS 14.3 and later) to integrate another analytics solution.

Register the Analytics module

Before you setup the Analytics module, you must register it in Google Analytics. You must register when you are setting up Analytics for the first time.

Perform the following:

  1. In Sitefinity CMS backend, navigate to Administration » Settings » Advanced » WebSecurity » RefererValidation.
  2. In Trusted locations input field, enter accounts.google.com and click Save changes.
  3. Log in to your Google Analytics account.
  4. Go to https://code.google.com/apis/console/.
  5. In the toolbar, click CREATE PROJECT.
  6. Choose a name for the project and click Create.
  7. In the toolbar, click on the Notification icon and then click on your newly created project.
  8. In the Google APIs screen, in the APIs section, click Go to APIs overview.
  9. Open the Library tab page.
  10. Under Other, select Google Analytics API.
  11. In the toolbar, click ENABLE.
  12. In the upper-right corner, click Create credentials.
  13. Under Add credentials to your project, click Cancel.
  14. On the top of the screen, expand Create credentials and click OAuth Client ID.
  15. In the upper-right corner, click Configure consent screen.
  16. Select the user type.
    You can select External for testing purposes.
  17. Click Create.
  18. Fill out the required information, such as App name, User support email, and Developer contact information.
    For more information, see Google's documentation about creating the consent screen.
  19. Go back to the Credentials page, expand Create credentials again, and click OAuth client ID.
  20. Select Web application.
  21. Under Authorized JavaScript origins, enter http://<your website>
  22. Under Authorized redirect URIs, enter http://<your website>/Sitefinity/marketing/Analytics
  23. Click Create.
  24. Copy the generated Client ID and the Client secret.

RESULT: You have registered the Analytics module, you can continue to setup the Analytics module in Sitefinity CMS backend.

Set up the Analytics module

PREREQUISITES: Before you setup the Analytics module, you must have added and configured the Google Analytics widget on at least one page, and the module must have already collected some data.

NOTE: When using the Google Analytics widget, the tracking code is always placed inside the <body> section of the page HTML. If you need to have the tracking code placed inside the <head> tag of the page HTML you must use a JavaScript widget and add the code manually. For more information see JavaScript widget.

When you open the Analytics module for the first time, you need to configure it to show data for the site you want.

To setup Analytics, perform the following:

  1. Login to Sitefinity CMS and click Marketing » Analytics.
    If you have not used the Analytics module before, the Configure Analytics link appears.
  2. Click Configure Analytics.
  3. Paste the Client ID and Client Secret that you have copied in Register the Analytics module » Step 18.

    NOTE: For specific Client ID and Client Secret, Sitefinity CMS stores only the latest generated refresh token. The reason is Google OAuth 2.0 limitation of 50 refresh tokens per user account. For more information, see Google official documentation about OAuth 2.0 expiration.

  4. Click Login to Google.
  5. Allow the created project to view and manage your Google Analytics data.
  6. In Available accounts, select the view that you have created to track this website.

    NOTE: If you have not set the tracking code, provided by Google Analytics on any page of your website or the page where you have set it has not been requested, Sitefinity CMS cannot find account and your domain in the list of Tracked domains.

  7. In Tracked domains, select the domain of your website.
    If you do not see the domain in the list, you can enter it manually.
  8. Click Save Settings.
    The Google Analytics tracking code appears.
  9. Click Save Settings.

RESULT: You can see website analytics data in the Analytics module.

If you are using the Analytics module, regardless of whether you have configured the Sitefinity CMS Analytics module or you use directly Google Analytics, you must modify the Google Tracking code, in order to incorporate the Tracking consent functionality.

IMPORTANT: If you do not setup the Analytics module to work with Tracking consent functionality, when the tracking consent is turned on for a domain, the Analytics module will continue gather statistics, regardless of the choice that the user has made. 

Perform the following:

  1. In Sitefinity CMS backend, navigate to Marketing » Analytics.
  2. In the left pane, under Customizations, click Settings.
  3. Expand Tracking code and copy the generated code.
    The code is generated by Sitefinity CMS, based on your Google Analytics tracking code. It includes the logic to turn on and off Analytics tracking when required.
  4. You must open every instance of the Google Analytics widget, dropped on a page or template, and replace the existing code with the copied one.
    For more information, see Google Analytics widget.

IMPORTANT: If you have not yet setup the Analytics module, it is important to use the tracking code generated by Sitefinity CMS and not the one from your Google Analytics account.

EXAMPLE: The following JavaScript is an example of the JavaScript that Sitefinity CMS generates when you configure the Analytics module. The script is generated regardless of whether Tracking consent is turned on or off:

The script above check, if the window.TrackingConsentManager object exists. The object is created by Sitefinity CMS when Tracking consent feature is enabled. If the object exists, the script subscribes for ConsentChanged event and invokes loadAndTrack function. If the object does not exist, the script loads the Analytics tracking code.

The loadAndTrack function does the following:

  • If canTrack argument is false, it cleans up the Google Analytics cookies.
  • If canTrack argument is true, load the Google Analytics script and reports a page visit, using the predefined Tracking ID. This example uses UA-00000000-0.

You can use the same approach to start or stop any client side tracker.

IMPORTANT: In case, you are using Google Analytics, but have not setup the Sitefinity CMS Analytics module, you must still use this tracking code. You can use the one, provided in the above example. You only need to change the tacking ID, which in this case is UA-00000000-0.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?