npm package: @primedata/analytics-js
Description: PrimeData JS SDK
Author: Apache Software Foundation
Latest version: 1.0.6

# Prime Data - Web JS SDK

> Prime Data - JS SDK tracking event

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![Build Status](https://img.shields.io/travis/unshiftio/url-parse/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/url-parse)
[![Security Rating](https://sonar.primedata.ai/api/project_badges/measure?project=Prime-SDK-JS&metric=security_rating&token=5d141e5d0006a88a480e50ac36adbce80c2bf87b)](https://sonar.primedata.ai/dashboard?id=Prime-SDK-JS)
[![Bugs](https://sonar.primedata.ai/api/project_badges/measure?project=Prime-SDK-JS&metric=bugs&token=5d141e5d0006a88a480e50ac36adbce80c2bf87b)](https://sonar.primedata.ai/dashboard?id=Prime-SDK-JS)
[![Lines of Code](https://sonar.primedata.ai/api/project_badges/measure?project=Prime-SDK-JS&metric=ncloc&token=5d141e5d0006a88a480e50ac36adbce80c2bf87b)](https://sonar.primedata.ai/dashboard?id=Prime-SDK-JS)
[![Coverage](https://sonar.primedata.ai/api/project_badges/measure?project=Prime-SDK-JS&metric=coverage&token=5d141e5d0006a88a480e50ac36adbce80c2bf87b)](https://sonar.primedata.ai/dashboard?id=Prime-SDK-JS)
[![Code Smells](https://sonar.primedata.ai/api/project_badges/measure?project=Prime-SDK-JS&metric=code_smells&token=5d141e5d0006a88a480e50ac36adbce80c2bf87b)](https://sonar.primedata.ai/dashboard?id=Prime-SDK-JS)
[![Quality Gate Status](https://sonar.primedata.ai/api/project_badges/measure?project=Prime-SDK-JS&metric=alert_status&token=5d141e5d0006a88a480e50ac36adbce80c2bf87b)](https://sonar.primedata.ai/dashboard?id=Prime-SDK-JS)


Prime Data Web Tracker Javascript Library
===========================================
This package can be used in a Javascript application to interact with Prime Data.

## Quick Start

### Install library
```bash
# npm
npm install @primedata/analytics-js
# yarn
yarn add @primedata/analytics-js
```

### Usage
Assuming some express-like web framework.
```ts
import Analytics from '@primedata/analytics-js'

// or, if you use require:
const Analytics = require('@primedata/analytics-js')

```

## Implicit page view event

In the initialize call, the tracker will generate an implicit page view event, which by default will be populated with 
the following information: 

```javascript
    window.digitalData.page = window.digitalData.page || {
        path: location.pathname + location.hash,
        pageInfo: {
            pageName: document.title,
            pageID : location.pathname + location.hash,
            pagePath : location.pathname + location.hash,
            destinationURL: location.href
        }
    }
```

Now if you want to provide your own custom page information for the initial page view, you can simply do it like this:

````javascript
// configuration
const primeJsOpts = {
    scope: '<SOURCE_KEY>',
    url: 'https://dev.primedata.ai/powehi',
    writeKey: '<WRITE_KEY>',
    initialPageProperties: {
        pageInfo: {
            destinationURL: window.location.href
        }
    },
    detectAdBlock: {
        enable: true,
        bannerMessage: "You are on the ad blocker. Our feature does not work well when your ad blocker is enabled on this site. To fully enjoy the service, please disable the ad blocker on our site. We guarantee that there will be no ads on our site.",
        closeButtonLabel: "close"
    }
}

// instantiation
const analytics = Analytics.initialize({"Prime Data": primeJsOpts})

````

Also note that the FIRST call to analytics.page() will be IGNORED because of this initial page view. This is the 
way that the Analytics.js library handles it. So make sure you are aware of this when calling it. This is to avoid having
two page views on a single call and to be compatible with old versions that did use the explicit call.

## Sending events

Here are some examples of sending events :

```javascript
analytics.page() // first call will be ignored as the initial page load is done in the initialize method

analytics.identify("af285fa381f77778ec427c01d48764b863c14705", {
    phone_number: '0985490107',
    full_name: 'Nguyen Le Phong',
    email: 'nguyenlephong@primedata.ai'
});

analytics.track('page_viewed', {
    title: 'How to Create a Tracking Plan',
    course: 'Intro to PrimeData CDXP'
});

```

As the PrimeData Tracker uses the Analytics.JS API, you can find more information about it [here](https://segment.com/docs/sources/website/analytics.js/). 
All methods can be used on `analytics` object, although not all event types are supported by PrimeData integration.