# How can we help?

## Overview

This article details how to set up Google Analytics for event tracking.

## Set up Google Analytics for event tracking

1. Log in to your Webforms Account.

2. Click your name in the upper right-hand corner, and select **Custom Javascript (parent)** in the drop down.

3. In the **After Document Ready** section, copy and paste in the code below, then scroll to the bottom and save your changes

```
api.GA = {};
api.GA.num_checks = 0;
api.GA.tracker = false;
api.GA.is_ga_classic = false;
api.GA.is_ga_universal = false;

//If you do not want us to auto-detect the tracking ID, then please enter yours for the UA_Tracking_ID value:
var UA_Tracking_ID = "UA-XXXXXXXX-Y";

var tryInitGAPrimer = function() {
    if (window.ga && window.ga.create) {
        api.GA.is_ga_universal = true;
        PRIMER_CUSTOM_JS.log('PRIMER: Using Google Universal Analytics');

if (!UA_Tracking_ID || UA_Tracking_ID === "UA-XXXXXXXX-Y") {
            api.GA.tracker = window.ga.getAll()[0];
        }
        else {
            api.GA.trackerArr = window.ga.getAll();
            if (api.GA.trackerArr && api.GA.trackerArr.length) {
                for (var i = 0; i < api.GA.trackerArr.length; i++) {
                    if (api.GA.trackerArr[i].get('trackingId') == UA_Tracking_ID) {
                        api.GA.tracker = api.GA.trackerArr[i];
                        PRIMER_CUSTOM_JS.log('PRIMER: Using GA Account: ' + api.GA.tracker.get('trackingId'));
                        break;
                    }
                }
            }
        }
    }
    else if (window._gaq && typeof window._gaq._getAsyncTracker == 'function') {
        api.GA.is_ga_classic = true;
        api.GA.tracker = window._gaq._getAsyncTracker();
        PRIMER_CUSTOM_JS.log('PRIMER: Using Google Classic Analytics');
    }
    else {
        PRIMER_CUSTOM_JS.log('PRIMER: Unable to find Google Analytics (Classic or Universal)');
    }
};

var initGAPrimerInterval = setInterval(function(){
    if (api.GA.tracker || api.GA.num_checks >= 10) {
        clearInterval(initGAPrimerInterval);
        if (api.GA.tracker) {
            PRIMER_CUSTOM_JS.log('PRIMER: Using Google Analytics, Tracker Found!');
        } else {
            PRIMER_CUSTOM_JS.log('PRIMER: NOT using Google Analytics (Classic OR Universal)!');
        }
    } else {
        api.GA.num_checks++;
        tryInitGAPrimer();
    }
}, 500);

api.GA.sendGAEvent = function(cat, act, lab, val) {
    if (api.GA.tracker) {
        if (typeof val !== 'undefined' && val !== null) {
            api.GA.tracker.send('event', cat, act, lab, val);
            PRIMER_CUSTOM_JS.log('PRIMER: Sent GA Event: ' + cat + ', ' + act + ', ' + lab + ', ' + val);
        } else {
            api.GA.tracker.send('event', cat, act, lab);
            PRIMER_CUSTOM_JS.log('PRIMER: Sent GA Event: ' + cat + ', ' + act + ', ' + lab);
        }
    }
    else {
        PRIMER_CUSTOM_JS.log('PRIMER: Did NOT send GA Event, tracker not found!');
    }
};

tryInitGAPrimer();
```

4. In the **After Display** section enter the following code:

```
var ga_label = lightbox_id;
if (PRIMER_LIGHTBOX_IDS && PRIMER_LIGHTBOX_IDS.hasOwnProperty(lightbox_id)) {
    ga_label = PRIMER_LIGHTBOX_IDS[lightbox_id];
}
if (PRIMER_LIGHTBOX_NAMES.hasOwnProperty(lightbox_id)) {
    ga_label += ' - ' + PRIMER_LIGHTBOX_NAMES[lightbox_id];
}
api.GA.sendGAEvent('Primer', 'Primer_Display', ga_label);
```

5. Add to Parent JS: **After** **Submit**.

```
var ga_label = lightbox_id;

if (PRIMER_LIGHTBOX_IDS && PRIMER_LIGHTBOX_IDS.hasOwnProperty(lightbox_id)) {
    ga_label = PRIMER_LIGHTBOX_IDS[lightbox_id];
}
if (PRIMER_LIGHTBOX_NAMES.hasOwnProperty(lightbox_id)) {
    ga_label += ' - ' + PRIMER_LIGHTBOX_NAMES[lightbox_id];
}
api.GA.sendGAEvent('Primer', 'Primer_Submit', ga_label);
```

6. Add to Parent JS: **After Redirect**.

```
var ga_label = lightbox_id;
if (PRIMER_LIGHTBOX_IDS && PRIMER_LIGHTBOX_IDS.hasOwnProperty(lightbox_id))
{
    ga_label = PRIMER_LIGHTBOX_IDS[lightbox_id];
}
if (PRIMER_LIGHTBOX_NAMES.hasOwnProperty(lightbox_id))
{
    ga_label += ' - ' + PRIMER_LIGHTBOX_NAMES[lightbox_id];
}
api.GA.sendGAEvent('Primer', 'Primer_Redirect', ga_label);
```

7. Add to Parent JS: **After Close**.

```
var ga_label = lightbox_id;

if (PRIMER_LIGHTBOX_IDS && PRIMER_LIGHTBOX_IDS.hasOwnProperty(lightbox_id)) {
    ga_label = PRIMER_LIGHTBOX_IDS[lightbox_id];
}
if (PRIMER_LIGHTBOX_NAMES.hasOwnProperty(lightbox_id)) {
    ga_label += ' - ' + PRIMER_LIGHTBOX_NAMES[lightbox_id];
}
api.GA.sendGAEvent('Primer', 'Primer_Close', ga_label);
```
