Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descrease webpack bundle size #131

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Node.js Package

on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
58 changes: 36 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
universal-analytics
Browser&NodeJS module for Google's Universal Analytics
=======

A node module for Google's [Universal Analytics](http://support.google.com/analytics/bin/answer.py?hl=en&hlrm=de&answer=2790010) tracking via the [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/).
https://www.npmjs.com/package/cross-ga

This module allows tracking data (or rather, users) from within a Node.js application. Tracking is initiated on the server side and, if required, does not require any more tracking in the browser.

[![npm version](https://badge.fury.io/js/universal-analytics.svg)](https://www.npmjs.com/package/universal-analytics) [![Build Status](https://travis-ci.org/peaksandpies/universal-analytics.png?branch=master)](https://travis-ci.org/peaksandpies/universal-analytics)
Fork of https://github.com/peaksandpies/universal-analytics

## Difference
Webpack bundle size decrease at 1.7MB
- `request` replaced to `cross-fetch`
- remove `uuid` dependency add https://github.com/heikomat/uuid-browser to lib
- remove `querystring` dependency add https://github.com/alexstep/cross-ga/blob/master/lib/querystring.js

### Before
![before](https://raw.githubusercontent.com/alexstep/cross-ga/master/why-fork/before.png)
### After
![after](https://raw.githubusercontent.com/alexstep/cross-ga/master/why-fork/after.png)


# About
A *browser and node* module for Google's [Universal Analytics](http://support.google.com/analytics/bin/answer.py?hl=en&hlrm=de&answer=2790010) tracking via the [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/).


# Table of Contents
Expand All @@ -31,17 +45,17 @@ This module allows tracking data (or rather, users) from within a Node.js applic

# Getting started

`universal-analytics` is installed and included like any other node module:
`cross-ga` is installed and included like any other node module:

```
$ npm install universal-analytics
$ npm install cross-ga
```

```javascript
var ua = require('universal-analytics');
var ua = require('cross-ga');

// Or with ES6 import
import ua from 'universal-analytics'
import ua from 'cross-ga'
```

Initialization expects at least your Google Analytics account ID:
Expand All @@ -50,13 +64,13 @@ Initialization expects at least your Google Analytics account ID:
var visitor = ua('UA-XXXX-XX');
```

This will create a `universal-analytics` Visitor instance that you can use and keep around to track a specific client (Not to be confused with the Google Analytics User ID, see [Setting persistent parameters](#setting-persistent-parameters) for more information on that). Since no client ID was specified in the constructor's arguments, a random UUID is generated. In case you have a client ID at hand, you can use that to create the visitor:
This will create a `cross-ga` Visitor instance that you can use and keep around to track a specific client (Not to be confused with the Google Analytics User ID, see [Setting persistent parameters](#setting-persistent-parameters) for more information on that). Since no client ID was specified in the constructor's arguments, a random UUID is generated. In case you have a client ID at hand, you can use that to create the visitor:

```javascript
var visitor = ua('UA-XXXX-XX', '6a14abda-6b12-4578-bf66-43c754eaeda9');
```

Starting with Universal Analytics, a UUID v4 is the preferred client ID format. It is therefor necessary to provide a UUID of such type to `universal-analytics`. However you can force custom client ID, passing `strictCidFormat: false` in the options:
Starting with Universal Analytics, a UUID v4 is the preferred client ID format. It is therefor necessary to provide a UUID of such type to `cross-ga`. However you can force custom client ID, passing `strictCidFormat: false` in the options:

```javascript
var visitor = ua('UA-XXXX-XX', 'CUSTOM_CLIENTID_1', {strictCidFormat: false});
Expand Down Expand Up @@ -125,7 +139,7 @@ visitor.pageview({dp: "/", dt: "Welcome", dh: "http://peaksandpies.com"}).send()

This code has the exact same effect as the one above. `dp`, `dt`, and `dh` (as in 'document path', 'document title' and 'document hostname') are the attribute names used by the Measurement Protocol.

It's mandatory to specify either the page path (`dp`) or document location (`dl`). Google Analytics can not track a pageview without a path. To avoid such erroneous requests, `universal-analytics` will deny `pageview()` tracking if the required parameters are omitted.
It's mandatory to specify either the page path (`dp`) or document location (`dl`). Google Analytics can not track a pageview without a path. To avoid such erroneous requests, `cross-ga` will deny `pageview()` tracking if the required parameters are omitted.

```javascript
var pagePath = null;
Expand Down Expand Up @@ -185,7 +199,7 @@ See also: [List of acceptable params](AcceptableParams.md).
## Event tracking


Tracking events with `universal-analytics` works like pageview tracking, only you have to provide different arguments:
Tracking events with `cross-ga` works like pageview tracking, only you have to provide different arguments:

```javascript
visitor.event("Event Category", "Event Action").send()
Expand Down Expand Up @@ -213,7 +227,7 @@ visitor.event("Event Category", "Event Action", "…and a label", 42, {p: "/cont
})
```

*Notice:* The page path attribute for the event is called `p` which differs from the `dp` attribute used in the pageview tracking example. `universal-analytics` is smart enough to use the `dp` attribute should you provide it instead of `p`.
*Notice:* The page path attribute for the event is called `p` which differs from the `dp` attribute used in the pageview tracking example. `cross-ga` is smart enough to use the `dp` attribute should you provide it instead of `p`.

In case this argument list is getting a little long, `event()` also accepts a params object like `pageview()`:

Expand All @@ -229,7 +243,7 @@ var params = {
visitor.event(params).send();
```

The category (`ec`) and the action (`ea`) are mandatory. Google Analytics will not track an event without them. To avoid such erroneous requests, universal-analytics will deny `event()` tracking if either attribute is omitted.
The category (`ec`) and the action (`ea`) are mandatory. Google Analytics will not track an event without them. To avoid such erroneous requests, cross-ga will deny `event()` tracking if either attribute is omitted.

```javascript
var action = null;
Expand Down Expand Up @@ -285,7 +299,7 @@ In case an additional item has to be added later on or daisy-chaining is not ava

visitor.item({ip: 100, iq: 1, ic: "item-41325", in: "Item 41325", iv: "XL", ti: "trans-12345"}).send()

The transaction ID (`ti`) is mandatory for both the transaction and the item. Google Analytics will not track e-commerce data without it. To avoid such erroneous requests, universal-analytics will deny `transaction()` and `item()` tracking if it is omitted.
The transaction ID (`ti`) is mandatory for both the transaction and the item. Google Analytics will not track e-commerce data without it. To avoid such erroneous requests, cross-ga will deny `transaction()` and `item()` tracking if it is omitted.

```javascript
var ti = null;
Expand Down Expand Up @@ -473,7 +487,7 @@ visitor.pageview("/").pageview("/contact").send()

Granted, the chance of this example actually happening in practice might be rather low.

However, `universal-analytics` is smart when it comes to daisy-chaining certain calls. In many cases, a `pageview()` call is instantly followed by an `event()` call to track some additional information about the current page. `universal-analytics` makes creating the connection between the two easy:
However, `cross-ga` is smart when it comes to daisy-chaining certain calls. In many cases, a `pageview()` call is instantly followed by an `event()` call to track some additional information about the current page. `cross-ga` makes creating the connection between the two easy:

```javascript
visitor.pageview("/landing-page-1").event("Testing", "Button color", "Blue").send()
Expand All @@ -487,7 +501,7 @@ visitor.event("Testing", "Button color", "Blue", {p: "/landing-page-1"}).send()

Daisy-chaining is context-aware and in this case placing the `event()` call right after the `pageview()` call results in the event being associated with the page path tracking in the `pageview()` call. Even though the attributes (`dp` and `p`) are different internally.

It also works when using a callback since the `this` inside the callback will be the `universal-analytics` Visitor instance:
It also works when using a callback since the `this` inside the callback will be the `cross-ga` Visitor instance:

```javascript
visitor.pageview("/landing-page-1", function (err) {
Expand Down Expand Up @@ -541,10 +555,10 @@ Then create a new view in Google Analytics of type 'Application'. You will then

# Session-based identification

In order to make session-based apps easier to work with, `universal-analytics` also provides a middleware that works in an Expressjs-style fashion. It will try to detect a client ID based on the `_ga` cookie used by the analytics.js client-side tracking. Additionally it will store the detected client ID in the current session to recognize the visitor later.
In order to make session-based apps easier to work with, `cross-ga` also provides a middleware that works in an Expressjs-style fashion. It will try to detect a client ID based on the `_ga` cookie used by the analytics.js client-side tracking. Additionally it will store the detected client ID in the current session to recognize the visitor later.

```javascript
var ua = require("universal-analytics");
var ua = require("cross-ga");
var express = require("express");

var app = express()
Expand All @@ -562,10 +576,10 @@ var visitor = ua.createFromSession(socket.handshake.session);

# Debug mode

`universal-analytics` is using the [`debug`](https://www.npmjs.com/package/debug) library. It can be instructed to output information during tracking by setting the `DEBUG` environment variable:
`cross-ga` is using the [`debug`](https://www.npmjs.com/package/debug) library. It can be instructed to output information during tracking by setting the `DEBUG` environment variable:

```
DEBUG=universal-analytics
DEBUG=cross-ga
```


Expand Down Expand Up @@ -596,7 +610,7 @@ The tracking methods have shortcuts:

The tests are written with [mocha](https://github.com/visionmedia/mocha) using [should](https://github.com/visionmedia/should.js) and [Sinon.JS](https://github.com/cjohansen/Sinon.JS).

Run them by executing the following commands in the `universal-analytics` directory:
Run them by executing the following commands in the `cross-ga` directory:

```
$ npm install
Expand Down
16 changes: 9 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

var request = require("request");
var uuid = require("uuid");
var querystring = require("querystring");
const crossfetch = require('cross-fetch');
const querystring = require("./querystring");
const uuid = require('./uuid');


var utils = require("./utils");
var config = require("./config");
Expand Down Expand Up @@ -436,12 +437,13 @@ Visitor.prototype = {

debug("%d: %o", count++, params);

var options = Object.assign({}, self.options.requestOptions, {
crossfetch(path, {
method: 'post',
body: getBody(params),
headers: self.options.headers || {}
});

request.post(path, options, nextIteration);
})
.then( ()=>{ nextIteration() } )
.catch(nextIteration)
}

function nextIteration(err) {
Expand Down
Loading