#Segment Analytics

Segment.io is collecting customer data with one API and send it to hundreds of tool for analytics, marketing and data 
warehousing. Currently we are using only one analytic tool and if we have to add one more analytic tool than we have to
make changes in existing code so instead of that Segment.io (Segment.io Analytics JS) provide solution for that. 
[Segment.io Quickstart Guide Here](https://segment.com/docs/sources/website/analytics.js/quickstart/)
The orchard segment app lives at frontend/js/apps/segment-analytics.js

## [Event Tracking](https://segment.com/docs/sources/website/analytics.js/#track)

Event tracking is more about showing feature usage than it is about logging for data mining. However, there are some options for logging actual attributes such as name or id or even some json that represents a model if needed. Large unreadable JSON blobs are not generally a good idea though because this isn't really a data warehouse, and searching JSON in the Google Analytics interface is suboptimal.

Here is how they break down an `event`:

* **Category** : usually the controller name e.g.  `Marketplace`, `Analytics Index`, `Analytics Overview`
* **Action** : the part of the feature you are trying to capture usage for eg: `Change Calendar`, `Select By`, `Launch App`, `Uninstall App`
* **Label** : ideally this is meant to be a label for the next field, "Value" such as label: `artist`, value: `artist_id`. But if you want more information than just name and id, you can technically put JSON in it.
* **Value** : this field is required to be an integer. that can be a 0/1 state, or a model id or any number value. 
* **labelType** : this field is required to be a string. It can be either `vendor` or `subaccount`.

### Javascript Based Tracking:

Requires that the Segment Analytics [copy/paste code](https://segment.com/docs/sources/website/analytics.js/quickstart/) has been executed. 
A basic call to track might look like 

```
  analytics.track(event, {
     category: category,
     action: action,
     label: label,
     value: value
 });
 ```


###HTML Based Tracking

By using some DOM element attributes, event tracking can be enabled if the `segment-analytics` js app has been loaded on that page. This is done like:
   
```
<a|button|any
    class="site-analytics"
    data-event="click|mouseover|any"
    data-event-category="Category Name"
    data-event-action="Action"
    data-event-label="My Label"
    data-event-value="76|number"
</a>
```
  
##[Page Tracking](https://developers.google.com/analytics/devguides/collection/analyticsjs/pages)

Since segment-analytics.js marks page views for all pages wherever it is being loaded.
A basic page method definition might look like:
```
analytics.page([category], [name], [properties], [options], [callback]);
```

##Debugging and Testing

Segment provides live debugger that lets you see every API call you make from your website or mobile app in real-time as they hit segment servers. 
We can search for just the event we want to investigate with the new filtering feature available on debugger.
Calling the debug method will turn on debug mode, logging helpful messages to the console.
```
analytics.debug();
```

##User Tracking

 From your Segment Dashboard, open the Integrations page and select the Google Analytics Integration, then Advanced Options. Locate Custom Dimensions and declare the mapping for google analytics custom dimensions.

Using different, we track pageviews by userId, labelId, labelType and impersonatorUserId. 
These four things are being mapped to Google's dimensions as:

* `dimension1` : `userId` (orchadmin user id in OA, vendor contact id in Workstation)
* `dimension2` : `labelId` (always in Workstation)
* `dimension3` : `impersonatorUserId` (if the session is impersonated by orchadmin user)
* `dimension4` : `labelType` (labelType if vendor or subaccount)

