AnalyticsAdapter

AnalyticsAdapter

new AnalyticsAdapter()

Adapter for Google Analytics

Source:

Methods

(static) init(options)

Initialize Analytics and set up the general configurations.

Source:
Parameters:
Name Type Description
options Object

(see attributes below)

Name Type Attributes Default Description
enabled boolean <optional>
true

enable/disable tracking on Google Analytics

analyticsID String <optional>
null

analyticsID to initialize GA library (it is used for ga('create', analyticsID, 'auto'))

logger Object <optional>
Object()

logging methods to use (see example below), if undefined there will be no logs

dimensions Array.<Object> <optional>
Object()
Name Type Description
key

custom dimensions name

value

custom dimensions value

Examples
// Logger
// Analytics with console as logger
 AnalyticsAdapter.init({
     enabled: true,
     analyticsID: 'UA-123456789-1',
     logger: console,
     dimensions: {
         'UserStatus': 1,
         'AccessType': 2,
         'Valuable': 5,
         'Action': 8,
         'PaymentType': 11
     }
 });
// Analytics with no logs
 AnalyticsAdapter.init({
     enabled: true,
     analyticsID: 'UA-123456789-1',
     dimensions: {
         'UserStatus': 1,
         'AccessType': 2,
         'Valuable': 5,
         'Action': 8,
         'PaymentType': 11
     }
 });
// Custom Dimensions
// In analytics initialization phase, you have to define all custom dimensions
// that you will use in your applications.

// Init method doesn't set custom dimension on Google Analytics, it only saves
// custom dimension for future use (for example in event tracking).

// You have to pass a pair (custom dimension name, slot id), where slot id is the slot
// of the custom dimension assigned from Google Analytics before.

// In this example, I set two custom dimensions ("UserStatus" with slot number 1
// and "Valuable" with slot number 5):
AnalyticsAdapter.init({
    enabled: true,
    dimensions: {
        'UserStatus' : 1,
        'Valuable' : 5
    }
});

(static) setDimension(dimensions)

Set a user/session (not hit) custom dimension.

Source:
Parameters:
Name Type Description
dimensions Array.<Object>
Name Type Description
key

custom dimensions name

value

custom dimensions value

Example
// The custom dimension has to be defined in init method before
// and, after, you have to use the same custom dimension name.

// For example, in the following code, I set UserStatus on slot number 1 and
// assign it the logged value:

// before, I save UserStatus custom dimension with slot "1"
Analytics.init({
    dimensions: {
        'UserStatus' : 1
    }
});

// after, I set custom dimension with the logged value:
AnalyticsAdapter.setDimension({
    'UserStatus' : 'logged'
});

(static) setId(id)

Set Analytics user id

Source:
Parameters:
Name Type Description
id string

user id

Example
AnalyticsAdapter.setId("123rgr");

(static) trackEvent(options)

Track an event

Source:
Parameters:
Name Type Description
options Object

(see attributes below)

Name Type Description
category string

event category

action string

event action

label string

event label

value number

event value

dimensions Array.<Object>
Name Type Description
key

custom dimensions name

value

custom dimensions value

Example
// Note: the custom dimension (in this example Valuable)
// has to be defined in the init method first and you have to use the same custom dimension name.
AnalyticsAdapter.trackPage({
    category: 'UI',
    action: 'open',
    label: 'menu',
    value: 7,
    dimensions: {
        'Valuable': 'yes'
    }
});

(static) trackPage(options)

Track pageview events with optional custom dimensions

Source:
Parameters:
Name Type Description
options Object

The trackPage options

Name Type Description
page string

The page name

title string

The page title

dimensions Array.<Object>
Name Type Description
key

custom dimensions name

value

custom dimensions value

Example
// Note: the custom dimension (in this example Valuable)
// has to be defined in the init method first and you have to use the same custom dimension name.
AnalyticsAdapter.trackPage({
    page: '/home',
    title: 'Home Page',
    dimensions: {
        dimensionOne: 'logged',
        dimensionFour: 'premium',
    }
});