TokenException: Error occurred while obtaining a token for portalId


Your DNN error logs are displaying the following error:

Error occurred while obtaining a token for portalId [X] and application Analytics

This log entry may appear when trying to create or edit new pages, or when first enabling Google Analytics. It may be accompanied by errors in the UI with similar wording. This can be caused by a mismatch between site analytics settings in the database and the token used for authentication.



Though this error often occurs after an update, it can occur on its own. This error is most frequently seen in Evoq versions 9.1.x.

To address this issue, you will need:

  • Access to a SuperUser account.
  • Access to SQL console or SQL Server
  • Access to the file structure for your site



The solution to this issue involves SQL modification of the site database. As with any solution of this type, it is strongly recommended that you make a complete database backup and test the procedure in a staging environment before attempting it on the production site.

  1. Log into SSMS or use the SQL Console to run the following commands against the site database:
    UPDATE HostSettings
    SET SettingValue = 'False' WHERE SettingName IN ('AnalyticsEnabled')
    UPDATE PortalSettings
    SET SettingValue = 'False' WHERE SettingName IN ('AnalyticsEnabled')
    DELETE PortalSettings
    WHERE SettingName IN ('AnalyticsAccountCode', 'AnalyticsClientCredentials')
  2. Make a backup of the file ~\Portals\_default\Licenses\Microservices.resources, then delete it.
  3. Clear your site cache (PersonaBar > Settings > Servers > Clear Cache)
  4. While still on your site, hard-refresh your browser (CTRL + F5 on Windows, CMD + Shift + R on Mac)
  5. Go to PersonaBar > Site Analytics. A frame should pop up prompting you to enable the feature. Click next through to the end then Enable Feature.


At this point, you should test the solution. The steps listed above usually solve the problem. If it is still occurring, there is a workaround that can be followed below. This workaround will not fix the Google Analytics integration in DNN Evoq, but it will ensure data is being passed to Google Analytics so that tracking data isn't lost. Tracking data will still be viewable normally through Google's UI.

  1. Navigate to Settings > Site Settings > Site Behavior > Default Pages
  2. Add the following Code Snippet to HTML Page Header Tags:
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

      ga('create', 'UA-###########', 'auto');
      ga('send', 'pageview');

  3. Replace UA-########### with the Google Analytics Tracking ID, which can be found in your Google Analytics account.
  4. Click Save, then clear your site cache and restart the application if possible.



  1. Perform the task that previously prompted the log error. If the log errors were created on a time interval rather than per-action, give the site an appropriate amount of time to generate the error.
  2. Check the error logs. No errors should be present from the time after the fix was applied.
  3. Data should be tracked through Google Analytics



Please sign in to leave a comment.