Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Custom Dimension in Google Analytics using Angulartics

I am using Angulartics for track some information to Google Analytics from AngularJS.

I need to set a Custom Dimension, I need to do something like this but using Angulartics third party.

ga('set', 'dimension5', 'custom data');

In the documentation of Angulartics I just saw pageTrack() or eventTrack() methods. So I don't if it's possible?

like image 610
Adriano Peluche Fazio Avatar asked Mar 15 '23 17:03

Adriano Peluche Fazio


2 Answers

Angulartics allows to set Custom Dimensions and Custom Metrics via setUserProperties({'dimension' + index : 'dimension-value'}). For Metrics you pass an object with the analogous data. The important thing is to use the prefix 'dimension' or 'metric' concatenated with the index of your custom dimension/metric. Indices for free users are [0,19], for premium users [0,199].

Valid examples of setting custom dimensions/metrics are:

$analytics.setUserProperties({'dimension1': 'dimension1-value'});

$analytics.setUserProperties({'dimension2': 'dimension2-value'});

$analytics.setUserProperties({'dimension3': 'dimension3-value'}); ...

$analytics.setUserProperties({'metric1': 'metric1-value'});

$analytics.setUserProperties({'metric2': 'metric2-value'});

$analytics.setUserProperties({'metric3': 'metric3-value'});...

A look into angulartics-google-analytics' source code shows:

$analyticsProvider.registerSetUserProperties(function (properties) {
  // add custom dimensions and metrics
  setDimensionsAndMetrics(properties);
});

function setDimensionsAndMetrics(properties) {
    if (window.ga) {
      // add custom dimensions and metrics
      for(var idx = 1; idx<=200;idx++) {
        if (properties['dimension' +idx.toString()]) {
          ga('set', 'dimension' +idx.toString(), properties['dimension' +idx.toString()]);
        }
        if (properties['metric' +idx.toString()]) {
          ga('set', 'metric' +idx.toString(), properties['metric' +idx.toString()]);
        }
      }
    }
  }

See Allow setting dimensions/metrics via setUserProperties()

like image 174
Tob Avatar answered Mar 17 '23 05:03

Tob


As of version 0.1.2 angulartics.google.analytics supports setting custom dimensions for both page and event tracking.

You can set custom dimensions like this

$analytics.setUserProperties({dimension1: 'myValue'});
like image 38
Thomas Inman Avatar answered Mar 17 '23 06:03

Thomas Inman