Comparing version 1.0.1 to 2.0.0
@@ -1,6 +0,13 @@ | ||
export interface GtagConfigurationOptions { | ||
sendDefaultPageView?: boolean; | ||
transportType?: 'beacon' | 'xhr' | 'image'; | ||
export interface GtagConfigOptions { | ||
send_page_view?: boolean; | ||
transport_type?: 'beacon' | 'xhr' | 'image'; | ||
groups?: string; | ||
[prop: string]: any; | ||
} | ||
/** | ||
* Only injects the google analytics gtag lib without calling config. | ||
* @param trackingId The ga tracking id. | ||
*/ | ||
export declare function onlyInstallGtag(trackingId: string): void; | ||
/** | ||
* Injects the google analytics gtag lib. | ||
@@ -10,7 +17,5 @@ * @param trackingId The ga tracking id. | ||
*/ | ||
export declare function installGtag(trackingId: string, options?: GtagConfigurationOptions): void; | ||
export declare function installGtag(trackingId: string, options?: GtagConfigOptions): void; | ||
export declare function gtagRaw(...params: any[]): void; | ||
export declare function gtag(command: 'config', trackingId: string, options?: { | ||
[prop: string]: any; | ||
}): void; | ||
export declare function gtag(command: 'config', trackingId: string, options?: GtagConfigOptions): void; | ||
export declare function gtag(command: 'set', options: any): void; | ||
@@ -21,2 +26,3 @@ export declare function gtag(command: 'event', action: 'page_view', options?: { | ||
page_path?: string; | ||
send_to?: string; | ||
}): void; | ||
@@ -23,0 +29,0 @@ export declare function gtag(command: 'event', action: 'exception', options?: { |
const anyWindow = window; | ||
const defaultGtagConfigurationOptions = { | ||
sendDefaultPageView: true, | ||
send_page_view: true, | ||
}; | ||
/** | ||
* Injects the google analytics gtag lib. | ||
* Only injects the google analytics gtag lib without calling config. | ||
* @param trackingId The ga tracking id. | ||
* @param options The options to configure. | ||
*/ | ||
function installGtag(trackingId, options = {}) { | ||
options = Object.assign(Object.assign({}, defaultGtagConfigurationOptions), options); | ||
const gtagOptions = { | ||
send_page_view: options.sendDefaultPageView, | ||
}; | ||
if (options.transportType) { | ||
gtagOptions.transport_type = options.transportType; | ||
} | ||
const scriptId = 'ga-gtag'; | ||
if (document.getElementById(scriptId)) { | ||
return; | ||
} | ||
function onlyInstallGtag(trackingId) { | ||
const { head } = document; | ||
const script = document.createElement('script'); | ||
script.id = scriptId; | ||
script.type = 'text/javascript'; | ||
@@ -31,4 +18,13 @@ script.async = true; | ||
gtagRaw('js', new Date()); | ||
gtag('config', trackingId, gtagOptions); | ||
} | ||
/** | ||
* Injects the google analytics gtag lib. | ||
* @param trackingId The ga tracking id. | ||
* @param options The options to configure. | ||
*/ | ||
function installGtag(trackingId, options) { | ||
onlyInstallGtag(trackingId); | ||
options = Object.assign(Object.assign({}, defaultGtagConfigurationOptions), options); | ||
gtag('config', trackingId, options); | ||
} | ||
function gtagRaw(...params) { | ||
@@ -42,2 +38,2 @@ anyWindow.dataLayer.push(arguments); | ||
export { gtag, gtagRaw, installGtag }; | ||
export { gtag, gtagRaw, installGtag, onlyInstallGtag }; |
@@ -9,24 +9,11 @@ (function (global, factory) { | ||
const defaultGtagConfigurationOptions = { | ||
sendDefaultPageView: true, | ||
send_page_view: true, | ||
}; | ||
/** | ||
* Injects the google analytics gtag lib. | ||
* Only injects the google analytics gtag lib without calling config. | ||
* @param trackingId The ga tracking id. | ||
* @param options The options to configure. | ||
*/ | ||
function installGtag(trackingId, options = {}) { | ||
options = Object.assign(Object.assign({}, defaultGtagConfigurationOptions), options); | ||
const gtagOptions = { | ||
send_page_view: options.sendDefaultPageView, | ||
}; | ||
if (options.transportType) { | ||
gtagOptions.transport_type = options.transportType; | ||
} | ||
const scriptId = 'ga-gtag'; | ||
if (document.getElementById(scriptId)) { | ||
return; | ||
} | ||
function onlyInstallGtag(trackingId) { | ||
const { head } = document; | ||
const script = document.createElement('script'); | ||
script.id = scriptId; | ||
script.type = 'text/javascript'; | ||
@@ -38,4 +25,13 @@ script.async = true; | ||
gtagRaw('js', new Date()); | ||
gtag('config', trackingId, gtagOptions); | ||
} | ||
/** | ||
* Injects the google analytics gtag lib. | ||
* @param trackingId The ga tracking id. | ||
* @param options The options to configure. | ||
*/ | ||
function installGtag(trackingId, options) { | ||
onlyInstallGtag(trackingId); | ||
options = Object.assign(Object.assign({}, defaultGtagConfigurationOptions), options); | ||
gtag('config', trackingId, options); | ||
} | ||
function gtagRaw(...params) { | ||
@@ -52,2 +48,3 @@ anyWindow.dataLayer.push(arguments); | ||
exports.installGtag = installGtag; | ||
exports.onlyInstallGtag = onlyInstallGtag; | ||
@@ -54,0 +51,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "mr-gtag", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Easy, strong typed, and a modern way to use google analytics gtag lib.", | ||
@@ -11,3 +11,4 @@ "main": "dist/umd/index.js", | ||
"build": "rollup -c rollup.config.es.js && rollup -c rollup.config.umd.js", | ||
"lint": "tslint src/**/*.ts" | ||
"lint": "tslint src/**/*.ts", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -14,0 +15,0 @@ "keywords": [ |
@@ -13,3 +13,3 @@ # mr-gtag | ||
``` | ||
npm i --save mr-gtag | ||
npm i mr-gtag | ||
``` | ||
@@ -19,3 +19,3 @@ | ||
First, installing gtag injects the lib: | ||
First, calling `installGtag` injects the lib: | ||
```ts | ||
@@ -27,3 +27,3 @@ import { installGtag } from 'mr-gtag'; | ||
This is equivalent to `gtag('config', trackingId, ...);`. | ||
This injects the lib and then calls what is equivalent to `gtag('config', trackingId, ...);`. | ||
@@ -35,3 +35,5 @@ Some options are available here: | ||
installGtag(trackingId, { | ||
sendDefaultPageView: false, // If set to false, tells gtag to not send the default pageview event. Default is true. | ||
send_page_view: false, // If set to false, tells gtag to not send the default pageview event. Default is true. | ||
user_id: 'USER_ID', // You can set the user_id like this | ||
// Or any other option supported by the google gtag lib | ||
}); | ||
@@ -42,9 +44,2 @@ ``` | ||
Set a value: | ||
```ts | ||
import { gtag } from 'mr-gtag'; | ||
gtag('set', { ... }) | ||
``` | ||
Send an event: | ||
@@ -59,7 +54,7 @@ ```ts | ||
## Development | ||
## Release notes | ||
``` | ||
npm i -g rollup | ||
npm i | ||
``` | ||
### 2.0.0 | ||
- BREAKING CHANGE: Options that `installGtag` accepts were renamed to be the exact same as the gtag ones. Be sure to update those. | ||
- We now correctly handle registering more than one tracking id. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7940
113
55