Google Analytics Dashboard Widget | Classic

The “Google Analytics” widget retrieves the Google Analytics data of the site associated with your stack using the Reporting API and displays the common metrics data (such as Pageviews, New Users) and user behaviors (such as average time on a page) on your Dashboard.

This step-by-step guide explains how to create the Google Analytics Dashboard Widget for your Dashboard.

The steps to be performed are as follows:

  1. Create Google Service Account Key
  2. Enable Google Analytics Reporting API
  3. Add User and Get View ID from Google Analytics
  4. Download Lambda function
  5. Deploy Lambda function
  6. Add Google Analytics Widget to Your Stack Dashboard
  7. Use the Extension
  1. Create Google Service Account Key

    In order to use this extension, you will need your Google Service account key. Google Service accounts let you fetch (or automate fetching of) your site's Google Analytics data.
    In case you don’t have a Service Account, read the instructions given in the Creating and managing service accounts document to create one. Make sure you create a Service account that is not assigned any role.
    Now that you have a Service Account, create a new Service account key as per the instructions outlined in the Creating and managing service account keys document. You need to select JSON when setting the key type


    Open the JSON file in a JSON editor, and note down the client_email value


    You will need this email address in Step 3 while configuring your Google Analytics account. You will also need the complete JSON code in Step 4 while configuring the Lambda function of your extension in Contentstack

  2. Enable Google Analytics Reporting API

    To enable the Google Analytics API, perform the steps given below:

    1. Go to your project’s API Dashboard page in Google Cloud Platform
    2. Click on the ENABLE APIS AND SERVICES button on the top

    3. In the search bar of the API Library page, type and select Google Analytics Reporting API 
    4. Click on the ENABLE button4._Enable_GA_Reporting_API.png
      Next, we need to add a user and get the View ID.
  3. Add User and Get View ID from Google Analytics

    A - Add New User and Assign Permission

    After logging in to your Google Analytics account, perform the steps given below:

    1. Click on Admin at the bottom on the left navigation panel and click on User Management
    2. In the Account Permissions page that opens, click on + (Plus icon) at top-right corner and select Add new users

    3. Enter the client email address that you retrieved in Step 1 within the JSON file and select the Read & Analyze checkbox under Permissions
    4. Click Add.

    B - Retrieve the View ID

    Navigate to the Admin page. Under the View section, click View Settings


    You will see the View details. Copy the value given under View I


  4. Download Lambda Function

    Download the Lambda function from GitHub. It contains all the required files.
    Then, navigate to your lambda function folder through a Terminal and install npm:

    npm install

    This downloads the required node modules within your lambda function.

    Open the service_account.json file within your lambda function folder and replace the existing content with the JSON content of the Service Key file that you extracted in Step 1. After that, zip your project file.

    Note: If you are using Linux, use the zip -r * command in your root folder to zip the file.

  5. Deploy Lambda Function

    When creating your Lambda function in AWS Lambda, upload your project zip file. Select 8.10.0 as the Node.js version.
    Under the Configuration section of your lambda function, add an API Gateway trigger. You will get an API Gateway URL, like this,
    Store this URL, as you will need this while configuring the extension in the next step.
    For additional security, you can configure an API Key for the API Gateway trigger and use this API Key when configuring your lambda function

  6. Add Google Analytics Widget to Your Stack Dashboard

    To add the Google Analytics widget, log in to your Contentstack account and perform the following steps:
    1. Go to your stack and click on CONTENT at the top
    2. Hover over the “Settings” icon and click on Extensions
    3. Click on the + Add Extension button on the top-right corner and select Create new
    4. On the Select Extension Type page, select Dashboard Widget. You will be directed to the Create New Dashboard page where you need to enter details in the fields as given below:
      1. Title: Provide a suitable title. For example, Website Analytics.
      2. Default width: Select Half Width.
      3. Hosting method: Select Hosted by Contentstack. As soon as you do this, you will see the Extension source code field below.
      4. External source code: In this field, you need to enter the extension code. Download the source code for the extension from Github.
      5. Config parameters: Enter the configuration details for the extension. Here, you need to mention the view ID that you retrieved and the AWS API gateway URL for the lambda function. If you have enabled API key for secured access, you need to mention that too.
            "url": "https://abcde1234.execute-api.
            "viewid": "111111111",
            "x-api-key": "SSS111Sss1ssSSsssS11s11SS11SS"
      6. Enable dashboard widget: Select this option to ensure all stack users are able to view the Google Analytics widget on their respective screens.
    5. Click Save to create your custom widget
  7. Use the Extension

    Once you have added your Dashboard Widget, navigate to the Dashboard by clicking on "Dashboard" link at the top.
    You should be able to see the Google Analytics widget added to your Dashboard


    Note: Make sure that the URL that you provide is live.

    You can view the analytics data of the last 1, 7, 15, 30, 180, or 365 day(s). Default value is 30 days.

Additional Resource: In Contentstack, you can view Google Analytics metrics for a particular entry using our Google Analytics Widget.

You can also integrate Google Analytics, via GTM (Google Tag Manager), into Contentstack. To do so, check out Google Analytics via GTM guide.

Was this article helpful?

Thanks for your feedbackSmile-icon

More articles in "Dashboard Widget Guide for External Apps"

On This Page