├── .travis.yml ├── .gitignore ├── .editorconfig ├── test ├── _setup.js └── index.js ├── package.json ├── license ├── src └── index.js ├── readme.md └── ganalytics.d.ts /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | *.lock 4 | *.log 5 | dist 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = tab 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.{json,yml,md}] 13 | indent_style = space 14 | -------------------------------------------------------------------------------- /test/_setup.js: -------------------------------------------------------------------------------- 1 | global.localStorage = { 2 | // 3 | }; 4 | 5 | global.location = { 6 | // 7 | }; 8 | 9 | global.document = { 10 | documentElement: {} 11 | // 12 | }; 13 | 14 | global._SENT_ = { 15 | // 16 | }; 17 | 18 | global.Image = function() { 19 | return global._SENT_; 20 | }; 21 | 22 | global.screen = { 23 | // 24 | }; 25 | 26 | global.innerWidth = 100; 27 | 28 | global.innerHeight = 100; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@stereobooster/ganalytics", 3 | "version": "3.3.0", 4 | "umd:name": "GAnalytics", 5 | "repository": "stereobooster/ganalytics", 6 | "description": "A tiny client-side module for tracking with Google Analytics", 7 | "unpkg": "dist/ganalytics.min.js", 8 | "module": "dist/ganalytics.mjs", 9 | "main": "dist/ganalytics.js", 10 | "types": "ganalytics.d.ts", 11 | "license": "MIT", 12 | "author": { 13 | "name": "Luke Edwards", 14 | "email": "luke.edwards05@gmail.com", 15 | "url": "lukeed.com" 16 | }, 17 | "scripts": { 18 | "build": "bundt", 19 | "pretest": "npm run build", 20 | "prepublish": "npm run build", 21 | "test": "tape test/*.js | tap-spec" 22 | }, 23 | "files": [ 24 | "*.d.ts", 25 | "dist" 26 | ], 27 | "keywords": [ 28 | "ga", 29 | "google", 30 | "analytics", 31 | "google-analytics", 32 | "ganalytics" 33 | ], 34 | "devDependencies": { 35 | "bundt": "^0.4.0", 36 | "tap-spec": "^5.0.0", 37 | "tape": "^4.9.1" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Luke Edwards (lukeed.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | var clientId = "ga:user"; 2 | 3 | export default function(ua, args, toWait) { 4 | args = Object.assign( 5 | { tid: ua }, 6 | args, 7 | { consent: 0 }, 8 | args && args.consent 9 | ? { 10 | cid: (localStorage[clientId] = 11 | localStorage[clientId] || Math.random() + "." + Math.random()) 12 | } 13 | : { uid: "anonymous", aip: 1 } 14 | ); 15 | 16 | function send(type, opts) { 17 | var k, 18 | d = document, 19 | str = "https://www.google-analytics.com/collect?v=1"; 20 | if (type === "pageview" && !opts) { 21 | opts = { 22 | dl: location.href, 23 | dt: d.title, 24 | dr: d.referrer, 25 | sr: screen.width + "x" + screen.height, 26 | vp: 27 | Math.max(d.documentElement.clientWidth, innerWidth || 0) + 28 | "x" + 29 | Math.max(d.documentElement.clientHeight, innerHeight || 0) 30 | }; 31 | } 32 | var obj = Object.assign({ t: type }, args, opts, { z: Date.now() }); 33 | for (k in obj) { 34 | // modified `obj-str` behavior 35 | if (obj[k]) str += "&" + k + "=" + encodeURIComponent(obj[k]); 36 | } 37 | new Image().src = str; // dispatch a GET 38 | } 39 | 40 | toWait || send("pageview"); 41 | 42 | return { args, send }; 43 | } 44 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ganalytics [![Build Status](https://travis-ci.org/stereobooster/ganalytics.svg?branch=master)](https://travis-ci.org/stereobooster/ganalytics) 2 | 3 | > A tiny (425B) client-side module for tracking with Google Analytics 4 | 5 | This is the fork of [lukeed/ganalytics](https://github.com/lukeed/ganalytics). 6 | 7 | This module exposes three module definitions: 8 | 9 | - **ES Module**: `dist/ganalytics.mjs` 10 | - **CommonJS**: `dist/ganalytics.js` 11 | - **UMD**: `dist/ganalytics.min.js` 12 | 13 | _Please see [Releases](https://github.com/stereobooster/ganalytics/releases) for changelog!_ 14 | 15 | ## Install 16 | 17 | ``` 18 | $ yarn add @stereobooster/ganalytics 19 | ``` 20 | 21 | ## Usage 22 | 23 | ```js 24 | import GAnalytics from "@stereobooster/ganalytics"; 25 | 26 | if (window.location.hostname !== "localhost" && !navigator.doNotTrack) { 27 | GAnalytics("UA-XXXXXXXX-X", { consent: localStorage["ga:consent"] }); 28 | } 29 | 30 | // or 31 | const ga = new GAnalytics("UA-XXXXXXXX-X", { aid: 1 }); 32 | // or 33 | const ga = GAnalytics("UA-XXXXXXXX-X", { aid: 1 }); 34 | 35 | ga.send("pageview"); 36 | ga.send("pageview", { dt: "Foobar", dp: "/foo" }); 37 | 38 | ga.send("event", { ec: "Video", ea: "Play", el: "Home Hero" }); 39 | ``` 40 | 41 | ## API 42 | 43 | ### GAnalytics(trackerID, options, toWait) 44 | 45 | #### trackerID 46 | 47 | Type: `String` 48 | 49 | Your Google Analytics tracker ID; eg `UA-XXXXXXXX-X` 50 | 51 | #### options.aip 52 | 53 | Type: `Integer`
54 | Default: `0` 55 | 56 | Anonymize the sender's IP address. See [Anonymize IP](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aip). 57 | 58 | #### options.an 59 | 60 | Type: `String` 61 | 62 | Specifies the application's name. See [Application Name](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#an). 63 | 64 | #### options.aid 65 | 66 | Type: `String` 67 | 68 | Specifies the application identifier. See [Application ID](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aid). 69 | 70 | #### options.aiid 71 | 72 | Type: `String` 73 | 74 | Specifies the application installer identifier. See [Application Installer ID](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aiid). 75 | 76 | #### options.av 77 | 78 | Type: `String` 79 | 80 | Specifies the application verison. See [Application Version](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#av). 81 | 82 | #### options.ds 83 | 84 | Type: `String` 85 | 86 | Indicates the data source type of the hit; eg `web` or `app`. See [Data Source](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds). 87 | 88 | #### toWait 89 | 90 | Type: `Boolean`
91 | Default: `false` 92 | 93 | When truthy, a `pageview` event **will not** be sent immediately upon initialization. 94 | 95 | ### ga.send(type, params) 96 | 97 | #### type 98 | 99 | Type: `String` 100 | 101 | The type of hit to send. Must be one of these: `pageview`, `screenview`, `event`, `transaction`, `item`, `social`, `exception`, or `timing`. 102 | 103 | #### params 104 | 105 | Type: `Object` 106 | 107 | The parameters to send based on the `type` of hit. 108 | 109 | Please follow the links for each available parameter set: 110 | 111 | - [Event](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#events) 112 | - [Exception](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#exception) 113 | - [Item](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ecomm) 114 | - [Pageview](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#content) 115 | - [Screenview](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cd) 116 | - [Social](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#social) 117 | - [Timing](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#timing) 118 | - [Transaction](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ecomm) 119 | 120 | For `pageview` hits _only_, if no `params` are provided, then the `document.title` and `location.href` values will be auto-filled. This allows you to send valid requests by writing: 121 | 122 | ```js 123 | ga.send("pageview"); 124 | // is the same as: 125 | //=> ga.send('pageview', { dt:document.title, dl:location.href }) 126 | ``` 127 | 128 | ## License 129 | 130 | MIT © [Luke Edwards](https://lukeed.com) 131 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | require("./_setup"); 2 | const test = require("tape"); 3 | const GA = require("../dist/ganalytics"); 4 | 5 | const KEY = "ga:user"; 6 | const isEmpty = x => Object.keys(x).length === 0; 7 | const isObject = x => Object.prototype.toString.call(x, "[object Object]"); 8 | const resetLocalStorage = () => (global.localStorage = {}); 9 | 10 | function isData(t, tid, evt, obj = {}) { 11 | let src = _SENT_.src; 12 | t.ok(src, "~> used an Image and set `src` attribute"); 13 | t.true( 14 | src.startsWith("https://www.google-analytics.com/collect?v=1"), 15 | "~~> sent to Google-Analytics API" 16 | ); 17 | t.true( 18 | src.includes(`&tid=${tid}`), 19 | "~~> includes the UA idenfitifer (`tid`)" 20 | ); 21 | t.true( 22 | src.includes(`&t=${evt}`), 23 | `~~> includes the event type: "${evt}" (\`t\`)` 24 | ); 25 | t.true( 26 | src.includes(`&cid=${localStorage[KEY]}`), 27 | "~~> includes the generated user-idenfitifer (`cid`)" 28 | ); 29 | t.true( 30 | src.includes("&z="), 31 | "~~> includes unique timestamp (`z` cache-buster)" 32 | ); 33 | for (let k in obj) { 34 | t.true( 35 | src.includes(`&${k}=${encodeURIComponent(obj[k])}`), 36 | `~~> includes the encoded(?) \`${k}\` value` 37 | ); 38 | } 39 | } 40 | 41 | test("exports", t => { 42 | t.is(typeof GA, "function", "exports a function"); 43 | 44 | let foo = new GA("", { consent: true }); 45 | t.true(isObject(foo), "works with `new` keyword"); 46 | t.is(foo.constructor.name, "Object", "~> is (simple) Object"); 47 | t.is(typeof foo.send, "function", "~> has `send` function"); 48 | t.true(isObject(foo.args), "~> has `args` object"); 49 | 50 | let bar = GA(); 51 | t.true(isObject(bar), "works without `new` keyword"); 52 | t.is(bar.constructor.name, "Object", "~> is (simple) Object"); 53 | t.is(typeof bar.send, "function", "~> has `send` function"); 54 | t.true(isObject(bar.args), "~> has `args` object"); 55 | 56 | t.end(); 57 | }); 58 | 59 | test(`localStorage['${KEY}']`, t => { 60 | resetLocalStorage(); 61 | 62 | let old = localStorage[KEY]; 63 | t.is(old, undefined, "(assumption) no known user"); 64 | 65 | let foo = GA("", { consent: true }); 66 | let uid = localStorage[KEY]; 67 | t.true(!!uid, `wrote a new "${KEY}" value to localStorage`); 68 | t.is(foo.args.cid, uid, "~> GA instance keeps a copy as `args.cid` key"); 69 | 70 | console.log(" "); // spacer 71 | 72 | let bar = new GA("", { consent: true }); 73 | t.is(localStorage[KEY], uid, `reuses same "${KEY}" value across instances`); 74 | t.is(bar.args.cid, foo.args.cid, "~> new GA instance has its own copy"); 75 | 76 | console.log(" "); // spacer 77 | resetLocalStorage(); 78 | 79 | t.is(localStorage[KEY], undefined, "(reset localStorage)"); 80 | 81 | let baz = new GA("", { consent: true }); 82 | t.not(localStorage[KEY], uid, `generated unique "${KEY}" value`); 83 | t.is( 84 | baz.args.cid, 85 | localStorage[KEY], 86 | "~> GA instance keeps a copy as `args.cid` key" 87 | ); 88 | 89 | t.end(); 90 | }); 91 | 92 | test("ga.send :: oncreate", t => { 93 | global._SENT_ = {}; // reset 94 | resetLocalStorage(); 95 | 96 | global.document.title = "Hello World"; 97 | global.location.href = "http://example.com/hello-world"; 98 | 99 | t.true(isEmpty(_SENT_), "(reset _SENT_)"); 100 | t.is(localStorage[KEY], undefined, "(reset localStorage)"); 101 | 102 | GA("UA-STRING", { consent: true }); 103 | t.false(isEmpty(_SENT_), "GA instance sent data immediately"); 104 | 105 | isData(t, "UA-STRING", "pageview", { 106 | dt: document.title, 107 | dl: location.href 108 | }); 109 | 110 | t.end(); 111 | }); 112 | 113 | test("ga.send :: toWait", t => { 114 | global._SENT_ = {}; // reset 115 | resetLocalStorage(); 116 | 117 | t.true(isEmpty(_SENT_), "(reset _SENT_)"); 118 | t.is(localStorage[KEY], undefined, "(reset localStorage)"); 119 | 120 | let ctx = GA("UA-STRING", { consent: true }, true); 121 | t.true(isEmpty(_SENT_), 'did NOT dispatch initial "pageview" event'); 122 | t.false(isEmpty(localStorage), "generates unique `cid` key in localStorage"); 123 | t.is(ctx.args.cid, localStorage[KEY], "~> and still stores it in instance"); 124 | 125 | t.end(); 126 | }); 127 | 128 | test("ga.send", t => { 129 | global._SENT_ = {}; // reset 130 | resetLocalStorage(); 131 | 132 | t.true(isEmpty(_SENT_), "(reset _SENT_)"); 133 | t.is(localStorage[KEY], undefined, "(reset localStorage)"); 134 | 135 | let foo = GA("UA-STRING", { consent: true }, true); 136 | 137 | global.document.title = "Custom Events"; 138 | global.location.href = "http://example.com/custom-events"; 139 | 140 | console.log(" "); // spacer 141 | 142 | let data = { 143 | ec: "Video", 144 | el: "Home Hero", 145 | ea: "Play" 146 | }; 147 | 148 | foo.send("event", data); 149 | isData(t, "UA-STRING", "event", data); 150 | 151 | t.false( 152 | _SENT_.src.includes(`&dt=`), 153 | "~~> does NOT auto-include the `document.title` when options are passed (`dt`)" 154 | ); 155 | t.false( 156 | _SENT_.src.includes(`&dl=`), 157 | "~~> does NOT auto-include the `location.href` when options are passed (`dl`)" 158 | ); 159 | 160 | console.log(" "); // spacer 161 | 162 | data = { dp: "/hello-world" }; 163 | foo.send("pageview", data); 164 | isData(t, "UA-STRING", "pageview", data); 165 | 166 | t.false( 167 | _SENT_.src.includes(`&dt=`), 168 | "~~> does NOT auto-include the `document.title` when options are passed (`dt`)" 169 | ); 170 | t.false( 171 | _SENT_.src.includes(`&dl=`), 172 | "~~> does NOT auto-include the `location.href` when options are passed (`dl`)" 173 | ); 174 | 175 | t.end(); 176 | }); 177 | -------------------------------------------------------------------------------- /ganalytics.d.ts: -------------------------------------------------------------------------------- 1 | declare module "ganalytics" { 2 | export type Number = string | number; 3 | export type Boolean = 1 | 0 | '1' | '0'; 4 | 5 | export type ProductAction = 'detail' | 'click' | 'add' | 'remove' | 'checkout' | 'checkout_option' | 'purchase' | 'refund'; 6 | 7 | export interface EventMap { 8 | 'event': EventParams, 9 | 'social': SocialParams, 10 | 'pageview': CommonParams, 11 | 'exception': ExceptionParams, 12 | 'screenview': ScreenviewParams, 13 | 'transaction': TransactionParams, 14 | 'timing': TimingParams, 15 | 'item': ItemParams, 16 | } 17 | 18 | export type EventOptional = 'pageview' | 'exception' | 'screenview'; 19 | 20 | export interface GAnalytics { 21 | send(type: K, params: EventMap[K]): void; 22 | send(type: EventOptional): void; 23 | } 24 | 25 | export interface CommonParams { 26 | /** 27 | * Anonymize the sender's IP address. 28 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aip 29 | * @example '0', 1 30 | */ 31 | aip?: Boolean; 32 | 33 | /** 34 | * The hit's data source type. 35 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds 36 | * @example 'call center', 'web' 37 | */ 38 | ds?: string; 39 | 40 | /** 41 | * The time delta (in milliseconds) between the hit's occurrence and when it was reported. 42 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#qt 43 | * @example 560, '560' 44 | */ 45 | qt?: Number; 46 | 47 | /** 48 | * The application or site owner's known identifier for the user. 49 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uid 50 | * @example 'as8eknlll', 'foobar' 51 | */ 52 | uid?: string; 53 | 54 | /** 55 | * Control the session duration. The session will begin or end with this hit. 56 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sc 57 | * @example 'start', 'end' 58 | */ 59 | sc?: 'start' | 'end'; 60 | 61 | /** 62 | * The IP address of the user in IPV4 or IPV6 format 63 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uip 64 | * @example '1.2.3.4' 65 | */ 66 | uip?: string; 67 | 68 | /** 69 | * The User Agent of the browser. 70 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ua 71 | * @example 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14' 72 | */ 73 | ua?: string; 74 | 75 | /** 76 | * The geographical location of the user. It should be a two-letter country code or a [Geographical ID](http://developers.google.com/analytics/devguides/collection/protocol/v1/geoid). 77 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#geoid 78 | * @example 'US', '21137' 79 | */ 80 | geoid?: string; 81 | 82 | /** 83 | * The referral source that brought traffic to the application. 84 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dr 85 | * @example 'http://example.com' 86 | */ 87 | dr?: string; 88 | 89 | /** 90 | * The campaign name. 91 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cn 92 | * @example '(direct)' 93 | */ 94 | cn?: string; 95 | 96 | /** 97 | * The campaign source. 98 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cs 99 | * @example '(direct)' 100 | */ 101 | cs?: string; 102 | 103 | /** 104 | * The campaign medium. 105 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cm 106 | * @example 'organic' 107 | */ 108 | cm?: string; 109 | 110 | /** 111 | * The campaign keyword. 112 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ck 113 | * @example 'Blue Shoes' 114 | */ 115 | ck?: string; 116 | 117 | /** 118 | * The campaign content. 119 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cc 120 | * @example 'content' 121 | */ 122 | cc?: string; 123 | 124 | /** 125 | * The campaign identifier. 126 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ci 127 | * @example 'ID' 128 | */ 129 | ci?: string; 130 | 131 | /** 132 | * The Google Ads ID. 133 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#gclid 134 | * @example 'CL6Q-OXyqKUCFcgK2goddQuoHg' 135 | */ 136 | gclid?: string; 137 | 138 | /** 139 | * Google Display Ads identifier. 140 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dclid 141 | * @example 'd_click_id' 142 | */ 143 | dclid?: string; 144 | 145 | /** 146 | * The screen resolution. 147 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sr 148 | * @example '800x600' 149 | */ 150 | sr?: string; 151 | 152 | /** 153 | * The viewport size. 154 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#vp 155 | * @example '123x456' 156 | */ 157 | vp?: string; 158 | 159 | /** 160 | * The character set used to encode the current page/document. 161 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#de 162 | * @default 'UTF-8' 163 | * @example 'UTF-8' 164 | */ 165 | de?: string; 166 | 167 | /** 168 | * The screen's color depth. 169 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sd 170 | * @example '24-bits' 171 | */ 172 | sd?: string; 173 | 174 | /** 175 | * The user's language. 176 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ul 177 | * @example 'en-us' 178 | */ 179 | ul?: string; 180 | 181 | /** 182 | * Specify whether Java was enabled. 183 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#je 184 | * @example '1', 0 185 | */ 186 | je?: Boolean; 187 | 188 | /** 189 | * Specify the version of Flash available. 190 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#fl 191 | * @example '10 1 r103' 192 | */ 193 | fl?: string; 194 | 195 | /** 196 | * Consider the hit as non-interactive. 197 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ni 198 | * @example 'd_click_id' 199 | */ 200 | ni?: Boolean; 201 | 202 | /** 203 | * The full URL (document location) of the document. 204 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dl 205 | * @example 'http://foo.com/home?a=b' 206 | */ 207 | dl?: string; 208 | 209 | /** 210 | * The document's hostname. 211 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dh 212 | * @example 'foo.com' 213 | */ 214 | dh?: string; 215 | 216 | /** 217 | * The path portion of the document's URL location. 218 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dp 219 | * @example '/foo' 220 | */ 221 | dp?: string; 222 | 223 | /** 224 | * The document's title. 225 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dt 226 | * @example 'Settings' 227 | */ 228 | dt?: string; 229 | 230 | /** 231 | * The document's content group. You may have up to 5 groupings, each of which cna have up to 100 groups. 232 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cg_ 233 | * @example 'news/sports' 234 | */ 235 | cg1?: string; 236 | 237 | /** 238 | * The document's content group. You may have up to 5 groupings, each of which cna have up to 100 groups. 239 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cg_ 240 | * @example 'news/sports' 241 | */ 242 | cg2?: string; 243 | 244 | /** 245 | * The document's content group. You may have up to 5 groupings, each of which cna have up to 100 groups. 246 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cg_ 247 | * @example 'news/sports' 248 | */ 249 | cg3?: string; 250 | 251 | /** 252 | * The document's content group. You may have up to 5 groupings, each of which cna have up to 100 groups. 253 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cg_ 254 | * @example 'news/sports' 255 | */ 256 | cg4?: string; 257 | 258 | /** 259 | * The document's content group. You may have up to 5 groupings, each of which cna have up to 100 groups. 260 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cg_ 261 | * @example 'news/sports' 262 | */ 263 | cg5?: string; 264 | 265 | /** 266 | * The ID of a clicked DOM element, used to disambiguate multiple links to the same URL for In-Page Analytics reports. 267 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#linkid 268 | * @example 'nav_bar' 269 | */ 270 | linkid?: string; 271 | 272 | /** 273 | * The application's name. 274 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#an 275 | * @example 'My App' 276 | */ 277 | an?: string; 278 | 279 | /** 280 | * The application identifier. 281 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aid 282 | * @example 'com.company.app' 283 | */ 284 | aid?: string; 285 | 286 | /** 287 | * The application's version. 288 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#av 289 | * @example '1.2' 290 | */ 291 | av?: string; 292 | 293 | /** 294 | * The application's installer identifier. 295 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aiid 296 | * @example 'com.platform.vending' 297 | */ 298 | aiid?: string; 299 | 300 | /** 301 | * The role of the products included in a git. If no Product Action is specified, all product definitions included with the hit will be ignored. 302 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#pa 303 | * @example 'detail' 304 | */ 305 | pa?: ProductAction, 306 | 307 | /** 308 | * The list or collection from which a product action occurred. This is an additional parameter that can be sent when Product Action is set to 'detail' or 'click'. 309 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#pal 310 | * @example 'Search Results' 311 | */ 312 | pal?: string, 313 | 314 | /** 315 | * The step number in a checkout funnel. This is an additional parameter that can be sent when Product Action is set to 'checkout'. 316 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cos 317 | * @example '2' 318 | */ 319 | cos?: Number, 320 | 321 | /** 322 | * Additional information about a checkout step. This is an additional parameter that can be sent when Product Action is set to 'checkout'. 323 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#col 324 | * @example 'Visa' 325 | */ 326 | col?: string, 327 | 328 | /** 329 | * Indicate the local currency for all transaction currency values. Value should be a valid ISO 4217 currency code. 330 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cu 331 | * @example 'EUR' 332 | */ 333 | cu?: string, 334 | 335 | /** 336 | * Specify the experiment ID that the user is exposed to, if any. It should be sent in conjunction with the `xvar` parameter. 337 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#xid 338 | * @example 'Qp0gahJ3RAO3DJ18b0XoUQ' 339 | */ 340 | xid?: string, 341 | 342 | /** 343 | * The experiment's version number, if any. It should be sent in conjunction with the `xid` parameter. 344 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#xvar 345 | * @example '1' 346 | */ 347 | xvar?: string, 348 | } 349 | 350 | export interface ScreenviewParams extends CommonParams { 351 | /** 352 | * The document's screen name. Required for mobile applications. Optional for web applications, defaulting to `dl` value. 353 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cd 354 | * @example 'High Scores' 355 | */ 356 | cd?: string, 357 | } 358 | 359 | export interface EventParams extends CommonParams { 360 | /** 361 | * The event category. 362 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ec 363 | * @example 'Category' 364 | */ 365 | ec: string; 366 | 367 | /** 368 | * The event action. 369 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ea 370 | * @example 'Action' 371 | */ 372 | ea: string; 373 | 374 | /** 375 | * The event label. 376 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#el 377 | * @example 'Label' 378 | */ 379 | el?: string; 380 | 381 | /** 382 | * The event's non-negative value. 383 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ev 384 | * @example 55 385 | */ 386 | ev?: Number; 387 | } 388 | 389 | export interface ExceptionParams extends CommonParams { 390 | /** 391 | * The exception's description. 392 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#exd 393 | * @example 'DatabaseError' 394 | */ 395 | exd?: string; 396 | 397 | /** 398 | * Specify if the exception was fatal. 399 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#exf 400 | * @example 'DatabaseError' 401 | */ 402 | exf?: Boolean; 403 | } 404 | 405 | export interface SocialParams extends CommonParams { 406 | /** 407 | * The social network. 408 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sn 409 | * @example 'facebook' 410 | */ 411 | sn: string; 412 | 413 | /** 414 | * The social interaction action; for example, on Facebook when a user clicks the 'Like' button, the social action is 'like'. 415 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sa 416 | * @example 'like' 417 | */ 418 | sa: string; 419 | 420 | /** 421 | * The social interaction's target, which is typically a URL but can be any text value. 422 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#st 423 | * @example 'http://foo.com' 424 | */ 425 | st: string; 426 | } 427 | 428 | export interface TimingParams extends CommonParams { 429 | /** 430 | * The user's timing category. 431 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#utc 432 | * @example 'category' 433 | */ 434 | utc: string; 435 | 436 | /** 437 | * The user's timing variable. 438 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#utv 439 | * @example 'lookup' 440 | */ 441 | utv: string; 442 | 443 | /** 444 | * The user's timing value. 445 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#utt 446 | * @example 123 447 | */ 448 | utt: Number; 449 | 450 | /** 451 | * The user's timing label. 452 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#utl 453 | * @example 'label' 454 | */ 455 | utl?: string; 456 | 457 | /** 458 | * The time (in milliseconds) it took for the page to load. 459 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#plt 460 | * @example 3554 461 | */ 462 | plt?: Number; 463 | 464 | /** 465 | * The time (in milliseconds) it took to perform a DNS lookup. 466 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dns 467 | * @example 43 468 | */ 469 | dns?: Number; 470 | 471 | /** 472 | * The time (in milliseconds) it took for the page to be downloaded. 473 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#pdt 474 | * @example 500 475 | */ 476 | pdt?: Number; 477 | 478 | /** 479 | * The time (in milliseconds) it took for any redirects to happen. 480 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#rrt 481 | * @example 500 482 | */ 483 | rrt?: Number; 484 | 485 | /** 486 | * The time (in milliseconds) it took for a TCP connection to be established. 487 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tcp 488 | * @example 500 489 | */ 490 | tcp?: Number; 491 | 492 | /** 493 | * The time (in milliseconds) it took for the server to respond after connecting. 494 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#srt 495 | * @example 500 496 | */ 497 | srt?: Number; 498 | 499 | /** 500 | * The time (in milliseconds) it took for the `document.readyState` to be 'interactive'. 501 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dit 502 | * @example 500 503 | */ 504 | dit?: Number; 505 | 506 | /** 507 | * The time (in milliseconds) it took for the `DOMContentLoaded` event to fire. 508 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#clt 509 | * @example 500 510 | */ 511 | clt?: Number; 512 | } 513 | 514 | export interface ItemParams extends CommonParams { 515 | /** 516 | * A unique identifier for the transaction. This value should be the same for both the 'transaction' hit and 'item' hits associated with the transaction. 517 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ti 518 | * @example 'OD564' 519 | */ 520 | ti: string; 521 | 522 | /** 523 | * The item's name 524 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#in 525 | * @example 'Shoe' 526 | */ 527 | in: string; 528 | 529 | /** 530 | * The price for a single item or unit. 531 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ip 532 | * @example 'OD564' 533 | */ 534 | ip?: Number; 535 | 536 | /** 537 | * The number of items purchased. 538 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#iq 539 | * @example 4 540 | */ 541 | iq?: Number; 542 | 543 | /** 544 | * The SKU or item code. 545 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ic 546 | * @example 'SKU47' 547 | */ 548 | ic?: string; 549 | 550 | /** 551 | * The category that the item belongs to. 552 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#iv 553 | * @example 'Blue' 554 | */ 555 | iv?: string; 556 | } 557 | 558 | export interface TransactionParams extends CommonParams { 559 | /** 560 | * A unique identifier for the transaction. This value should be the same for both the 'transaction' hit and 'item' hits associated with the transaction. 561 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ti 562 | * @example 'OD564' 563 | */ 564 | ti: string; 565 | 566 | /** 567 | * The store or affiliation from which this transaction occurred. 568 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ta 569 | * @example 'Member' 570 | */ 571 | ta?: string; 572 | 573 | /** 574 | * The total revenue associated with the transaction. This value should include any shipping or tax costs. 575 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tr 576 | * @example '15.47' 577 | */ 578 | tr?: Number; 579 | 580 | /** 581 | * The total shipping cost of the transaction. 582 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ts 583 | * @example '3.50' 584 | */ 585 | ts?: Number; 586 | 587 | /** 588 | * The total tax of the transaction. 589 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tt 590 | * @example '11.20' 591 | */ 592 | tt?: Number; 593 | 594 | /** 595 | * The coupon redeemed with the transaction. 596 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tcc 597 | * @example 'SUMMER08' 598 | */ 599 | tcc?: string; 600 | } 601 | 602 | /** 603 | * @param trackerID Your Google Analytics tracker ID; eg UA-XXXXXXXX-X 604 | * @param options Options 605 | * @param toWait When truthy, a pageview event will not be sent immediately upon initialization. 606 | */ 607 | export default function (trackerID: string, options?: Partial, toWait?: boolean): GAnalytics; 608 | } 609 | --------------------------------------------------------------------------------