Implementing Analytics Using JavaScript

To begin using Analytics, data must be sent to a report suite to display in reporting.

The easiest and recommended way to send data to Analytics is by using Dynamic Tag Management. However, in some cases, you might want to implement Analytics using the older JavaScript method.

Note: This section describes the legacy method of implementing Analytics. All Analytics customers have access to Dynamic Tag Management which is the standard method to deploy Marketing Cloud tags.

Implementation Steps

To successfully implement a page with code to collect data, you must have access to your hosting servers to upload new content to your website. It is also useful to have an existing site to implement code.

The following steps walk you through a basic Analytics implementation.

Step Task Description
Download AppMeasurement for JavaScript and the Visitor ID service.

The download is available in Code Manager.

This download zip contains several files.AppMeasurement.js and VisitorAPI.js are the relevant files when implementing Analytics.

Set up the Marketing Cloud ID service.

(Formerly Visitor ID service.) See Set Up the Marketing Cloud ID Service for Analytics.

Update AppMeasurement.js.

Copy the Example AppMeasurement.js Code and paste it at the beginning of your AppMeasurement.js file. At a minimum, update the following variables:

  • s.account="INSERT-RSID-HERE"
  • s.trackingServer="INSERT-TRACKING-SERVER-HERE"
  • s.visitorNamespace = "INSERT-NAMESPACE-HERE"
  • s.visitor = Visitor.getInstance("INSERT-MCORG-ID-HERE")

See Correctly populate the trackingServer and trackingServerSecure variable or contact Client Care if you are unsure about any of these values. If they are not set correctly, data will not be collected by your implementation.

Host AppMeasurement.js and VisitorAPI.js.

These core JavaScript files must be hosted on a web server that is accessible to all pages on your site. You need the path to these files in the next step.

Reference AppMeasurement.js and VisitorAPI.js on all site pages.

Include the Visitor ID Service by adding the following line of code in the <head> or <body> tag on each page. VisitorAPI.js must be included before AppMeasurement.js:

<script language="JavaScript" type="text/javascript" 

Include AppMeasurement for JavaScript by adding the following line of code in the <head> or <body> tag on each page:

<script language="JavaScript" type="text/javascript" 
Update and deploy page code.

Copy the Example Page Code and paste it just after the opening <body> tag on each page you want to track. At a minimum, update the following variables:

  • var s=s_gi("INSERT-RSID-HERE")
  • s.pageName="INSERT-NAME-HERE" // for example, s.pageName=document.title
Use the DigitalPulse Debugger to verify that data is being sent.

Install the Adobe Debugger bookmarklet. After it is installed, load a page where you have deployed page code and then open the debugger. The debugger displays details about the collection data that was sent.


The JavaScript file is cached in the visitor's browser after it is initially loaded, and is typically downloaded no more than once per session. The file is not downloaded on each page, even though it is used by every page on the site. On most websites, users average more than a few page views per session, so transferring JavaScript that is used multiple times into this file can result in less overall downloaded data.

JavaScript for AppMeasurement Compression

If you are concerned about page weight (size) of H Code (AppMeasurement for JavaScript 1.0 is pre-compressed), Adobe recommends that you consider compressing the file using GZIP. GZIP is supported by all major browsers and offers better performance than JavaScript compression to compress and decompress the core s_code.js JavaScript file.

The following links help explain how you can use GZIP functionality on your site to handle compression of the s_code.js JavaScript code:

Apache Module mod_deflate

Enabling gzip compression with Tomcat and Flex