
# Analytics SDK

[![Coverage Status](https://coveralls.io/repos/github/mobify/analytics-sdk/badge.svg?branch=master&t=mFCY8c)](https://coveralls.io/github/mobify/analytics-sdk?branch=master)

```
                                                                                          
                                                                                          
                                                                                          
                                                                                          
                                                                                          
                                                                                          
                                                                                          
                                                                                          
                                     .......'.........                                    
                                 .,,'.                  ...                               
                               'c,.                        .                              
                             .oo;     ........                                            
                             coo'   .;looolcc:;;,,,;,;;,....  ..                          
                            dxoc' ,dOO0OOOOOOOOOOOOOO0000kxd, .'.                         
                           cO; 'cxKKKKKKKKKKKKKXXXXXXXXXXXXXXk,.                          
                          .d..'lOXXXXXXNNNNNNNNNNNNNNNNNNNNNNNK:.                         
                          :;::dOKkOxOOOOkxdxxxxdddddxxxxxxkkkkkd;.                        
                         .clokKKKOKkkkxkxxxxxxxxxkxkxxkkkkkkkOx0O0x.                      
                         kkKNNNXXxkkOOkkkkxxxxxxxxxxxxxxxxxkkOx0KNWO                      
                       .k0OXXNNXNOdkOkkkxxxxxxxxxxxxxxxxxxxxkOx0KXOc                      
                      'k0OKXXKXXNOdkOkkkkxxxxxxxxxxxxxxxxxxxkkx0Kd'.                      
                     cdOOKXXXXXXNOdkOkkkkxxxxxxxxxxxxxxxxxxxkkx0Kxcl'                     
                    .dOOKXXXXXXXN0dkOOkkkkxxxxxxxxxxxxxxxxxxkkx0Kdld:                     
                   .oOK0XXXXXXXXNKxkOOkkkkkxxxxxxxxxxxxxxxxkkkxOKd;:;                     
                   :kKXKKXXXXXXNNXxkOOkkkkkkkkkkkkkkkkkkkkkkkOkOKdcco.                    
                   oOKNX0KXXXXXXNXxx0OOOOOkOOOOOOOOOOOOOOOOOOOkkOdxdo,                    
                   l00XXKKXXXXXXNNkx00000000000000000000000000OOOdxoo;                    
                   ;K0KXKKKKXXXXNN0xOOOOOOOOOOkkkkOOkkkkkkkkkkkxxxdooc,,'.                
                   'KKKKKKKKKXXXNNOoxxxxkkkkOOOOO000OOOOkOOOO00OkxxddOkdo,.               
                   .KKKKKKKKKXXXXNNXXXXXXKkkkkkkkkkxxxkxOXNNNWX0OkkxOKO00kl.              
                   .KKKKKXXXXXNNXNWWNNNNNKxdxxdkxkxxkdkxxNWWWNKKOOk0XK00000d,             
                    KKKKKXNXXXXXXXXNWWWWWXdxOkk0kOkkOkOOdXWWXxO0XK0NXX00000Kl             
                   .KKXXXXNWNX0O0000KNNWWNxdkkKO0XOX0OOkxKX0OxkXXKNNXKKK00KXo             
               ..,'.KKXXXXNWNXXXXNNNK000XNxcoolddodloocc:0000XWNXWNXKXX00KNK:             
             ;xkxdcdKXNXNNXNWNXXNNNNWWWNKKklodlodlxodddxOKKNWWNNWNXKXKKKKNNk.             
            ,lollxOK0XXKXNNXNWNXXNNWWWWWWWNX0KKKKXXK0Ok0XNNWWNNWNXKKKKKXNWKo.             
           ,dcl:lO0XKXXKKXNNXNWNNXNNNWWWWWWWWXKXNNNNXNNNNNWWNNWNXKKKKKXNWNOxokc.          
          .okOooxKXXXXNXKKXXNXXNWNNNNNNWWWWWWWNNNWWWWNNNNWWNNWNXKKKKXNNWWK0dcdx;,.        
         ,c0KK0XKKXXXXNNXKXXXXNXNWWNNNNNNNWWWWNNNNNNWNNNWWXNWNKKKKXXNWWWXX0KKo,;;;........
   .',,.,'lKXX0NNXXXKXNWNXKXXXXNNXNWWWNNNNNNNNNNNNXXXNNWWXXWXXKKXXNWWWWKXXNNXKc:codc;:lddd
;cl:,...oO0KKXKXNNXNNKKNWNXXXNNNNNNXNWWWWWWNNNNNNNNNNNWWNXNNXXXNWWWWWN0XNNNXNNOkdkNKxO0000
c'.....'OXXKKXXKNXNNNWXXNWNNNNNNNNNNXXNWWNWWWWWWNWWWWWWNNWNNNNWWWWWWXOXNNNXNNW0KkKWN0KKKXX
clxkkOkkXNXXKXXXNXXNNNWNKKWNWWWWNNNWWNKXNWWWWWWWWWWWWWWWWNNNWWWWWWW00NNNXXNXNNO0XXWWXXXNNN
KXXKKKOKNWNNXXNXNNXXXNNWNK0NWNWWWWWWWWNXKXNWWWWWWWWWWWWWWWWWWWWWWN0KNNNXXNNWN0OOXNWWXNNNNN
NNNXXXXXNWWNNXNXKNNXXXNNNWXOXWWWWWWWWWWWNXKNNWWWWWWNNWWWWWNNNNWWX0NNNNXNNWWXkKXONWWWNNWWWW
WWNNNXNNNWWNNNNXXKNNXXXNNNWN00NWWWWWWWWWWWXXXNNWWWWWNWWWNNNNNWWKKNNNXXNNWNOxXWKKWWWWNWWWWW
WNkd0XNWWWWWNNNNXXXNNXXNNNNWWKOXNWWWWWWWWWWWNNNNWWWWNNNNNNNNWN0XNNNNNNWWXdOWWN0NWWWWNWWWWW
WOddcocKWWWWWNNNNXXXNWNXNNNNWWXO0NWWWWWWWWWWWNNNWWWWWNNNNNNWX0XNNNNWWWNOxXWWWXKNWWWWXNXkdx
WKkxo;,:WNWWWWNXNNKK0XWNNNNNNNWN0OXNWWWWWWWWWWWNNWWWWWWNNNNO0NWWWWWWWKxONWWWWK0NNNKOOX0O0X
NNX0kl:kWNNWWWWNXNN0KO0NWNNNNNNNNKOXWWWWNNNWWWWWWNNWWWWNNX0XWWWWWWWXkxXWWWWWKkOOkdoo0NWWWW
NXXXXXNWWNKXNWWWXKNX00kONWWWNNNNNNXOKNWWWNNNNNWWWWNNWWNNNNWWWWWWWNOxKWWWWWWNddkxlccd00KNNX
WWNNNNNNNN0k0XNWNKKNKkkdkXWWWWWNNNNKOKNWWWWWNNNNNWWWWWNNWWWWWWNN0dONWWWWWWNxloddo;l0Kdoloc
NNWWWNXKKKXOxOKNNX0KNKxodxKWWWWWWWWWNXNNWWWWWWNNNNNWWWWWWWWWNX0xkXWWWWWNNXxcodolkxKXXxcclc
XNNWWNK00KXNOdk0XXKkXN0dooxKWWWWWWWWWNNNNWWWWWWWNNNNWWWWWWNXKOd0NWWWWNNNKdcodol:Ko:OO0oddk
```

_Portrait of one of the original [SDK](https://en.wikipedia.org/wiki/Sardaukar) member_


## Introduction

https://www.npmjs.com/package/mobify-analytics-sdk

Analytics SDK standardize how we capture analytics across different Mobify platform.  It contains a few parts:

* Config file
* Debug experience
* Test framework
* Deployment tool


## To get started

```
npm i
npm run build
npm run test
```

This will run the necessary tests.

To hot reload test in browser:

```
npm run build:watch &
npm run start:cold &
open http://localhost:8443/webpack-dev-server/test/index.html
```


### Building the analytics SDK javascript

The analytics SDK will read a specific config file that can be bundled.

To build the SDK, run:
```
npm run build
```

Which will generate the `analytics-sdk/index.js`.  The SDK can then be loaded on any webpage by doing:

```
<script src='analytics-sdk/index.js'></script>
```

> Note: this is for temporary demo but eventually this will be loaded by a.js directly

# Side loading script via bookmarklet

One can side-load the script in any page by running:

```
npm start
```

And you can load the script from [https://localhost:8443/public/bundle/index.js](https://localhost:8443/public/bundle/index.js)
which will have the hot-reload available.

Note that https is required for side-loading on https site.

And then use the following

<a href="javascript:(function(){ var scriptTag = document.createElement(%27script%27); scriptTag.src = %22https://localhost:8443/public/bundle/index.js%22; document.body.appendChild(scriptTag); })();">AnalyticSDK</a>

Just drag and drop this into the toolbar.


## Publishing

https://www.npmjs.com/package/mobify-analytics-sdk

This is published on merge to master branch.

To publish manually:

```
npm run build

# Remember to bump version in package.json

npm publish
```

Note that assets bundling should be the responsibility of the library requirer.


---------

## Analytics SDK config file

The config file is responsible for specifying what to capture on the client.

* The config file schema is defined in [sdkschema.js](sdkschema.js)
* Example config file is in [config/config.js](config/config.js).

To validate a config file, run:

```
npm run build
bin/validate-sdk.js config/config.js
```

These files also are responsible for generated the [documents for config](docs/config.md).



---------

## Generated Documentation

```
npm run docs:build
```

There are 2 main parts of documentation:

* [Main docs](docs/index.md) generated from `src/*`
* [Configuration specific docs](docs/config.md) generated from `config/config.js`
    - The config file serves as both validation test and documentation to ensure
      config is up to date.

The source of these docs are documented in `package.json` `docs:build` script.

The docs is generated into both Markdown (via `jsdoc2md`) and HTML format (via `jsdoc` - not checked into repo)