
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
@impervaos/google-analytics-spa
Advanced tools
Automatic Google Analytics reporting for your single page application, used with Redux in React, Angular, Vue
This library was created with Single Page Application architecture in mind. You can use it with ReactJs, Angular, Vue or just vanilla javascript code. Its goal is to provide as automatic as possible usage of Google Analytics for SPAs (Single Page Applications). So you could free yourself to deal with other tasks
Install the package
npm i @impervaos/google-analytics-spa
Add the following snippet in your index.html (or whatever page that is loaded first)***
<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
</script>
<script defer src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
Add the following in your first JSX / JS file (the root of your SPA application)
/* eslint-disable */
import { googleAnalyticsInit } from '@impervaos/google-analytics-spa';
import { createBrowserHistory } from 'history';
//[OPTIONAL] You dont have to use the 'history' package
// However if you do, page navigation would be traced and reported to GA automatically
const history = createBrowserHistory({ basename: '' });
//This is your GA application id.
//from https://analytics.google.com/analytics/web/#/.../admin/property/settings (your GA property page)
const myGaApplicationId = 'UA-XXXXXXX-XX';
//[OPTIONAL] The name of your tracker, in case you will be using multiple trackers
const myTrackerName = 'MyTrackerName';
//--------------------
//performance reporting configuration
// it can be either regex - and then only the requests whos urls can be matched using this regex will be reported
//for example: if my backend requests go to http://some.com/api/v1/getMyData we would set this parameter to /.*api\/v1/
// OR it can be an object {include: {regex}, initi: [{string}...{string}], category: {function}}
// * See types here: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/initiatorType
const performanceRegex = {include: /.*my_api.*/i, initiatorTypes: ['xmlhttprequest','fetch'], category: e => e.name.replace('.','_')};
//every request will also piggyback these dimensions with it
//For example: user email or any other custom dimension that you need to better track your application usage
//read more here https://support.google.com/analytics/answer/2709829?hl=en
const customDimensions = {
dimension1: MyUserDetails.email,
dimension2: MyUserSession.id,
dimension3: Date.now(), //example: session start timestamp
};
//[OPTIONAL] In case you would like to track your users using "GA User explorer" you need to provide GA
// with a unique identifier per user.
// If you would not provide this identifier, google will generate a random id
//GA tracker properties (https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference)
const properties = { userId: MyUserDetails.id }; //example reporting userId
googleAnalyticsInit( myGaApplicationId,
myTrackerName,
history,
performanceRegex,
properties,
customDimensions
);
***I decided not to embed the ga.js code, since google promissed to change it unexpectedly
/* eslint-disable */
import { tracker } from '@impervaos/google-analytics-spa';
function myComplicatedAction() {
try {
performSomeComplicatedAction();
} catch(e) {
//in case of an error we report it to google analytics
tracker().reportException(e.message, false);
return;
}
tracker().reportEvent( 'MY_CATEGORY', 'MY_ACTION_PERFORMED', 'my_label_text', 0 );
}
//manual page view reporting (i.e. reporting that navigation was done to page http://page.com/first)
tracker().reportPage( 'my site title', 'http://page.com/first' );
//navigating to another page in the application
// @impervaos/google-analytics-spa will report navigation to page called '/virtual/path' automatically instead of reporting navigation to '/test/path'
history.push( '/test/path', gaBuildPageViewState( 'TITLE', '/virtual/path', true ) );
import {reportGoogleAnalytics, GaReportingReduxMiddleware} from '@impervaos/google-analytics-spa';
const store = createStore(state => state, applyMiddleware(GaReportingReduxMiddleware));
//In order to use this annotation your code need to be able to process annotations
//So you might want to use something like https://babeljs.io/docs/en/babel-plugin-proposal-decorators
//In the future the decorators will be part of ES
class ReduxActions {
@reportGoogleAnalytics('MyCategory', null, 'my label', 1)
static doSomethingWithAnnotation() {
return {
type: 'DO_SOMETHING'
};
}
static doSomething() {
return {
type: 'DO_SOMETHING_ELSE'
};
}
}
//When this event is dispatched, an automatic even report will be sent to Google Analytics
//equal to: tracker().reportEvent('MyCategory','DO_SOMETHING', 'my label', 1);
store.dispatch(ReduxActions.doSomethingWithAnnotation());
//When this event is dispatched, an automatic even report still being sent to Google Analytics
//equal to: tracker().reportEvent('','DO_SOMETHING_ELSE', '', 0);
store.dispatch(ReduxActions.doSomething());
Kind: global instance method of tracker()
Summary: Reporting an event performed
Access: public
Params
string
- event categorystring
- event namestring
- label of the eventnumber
- how much this event worth (usually in USD)Kind: global class
Access: public
############## gaTracker.setCustomDimension
Kind: instance instance method of tracker() of GaTracker
Summary: sets a custom dimension on the fly. Read more here: https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Access: public
Params
number
- integer > 1 - maxMetrics allowed (usually 20)any
- value to be set inside dimension############## gaTracker.setCustomMetric
Kind: instance instance method of tracker() of GaTracker
Summary: sets a custom metric on the fly. Read more here: https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Access: public
Params
number
- integer > 1 - maxMetrics allowed (usually 20)any
- value to be set inside metric############## gaTracker.setUserId(identifier)
Kind: instance method of GaTracker
Summary: (Usually should not be used) Manually set user id (might be overriden by next requests)
Access: public
Params
string
- identifier that is used to identify this specific user across multiple sessions and / or devices############## gaTracker.reportDuration(duration, category, requestUrl, label) Not required by default
Kind: instance method of GaTracker
Summary: Manually report the duration of some action for tracking
Access: public
Params
number
- the number of time units that action tookstring
- perofmance event categorystring
- url of the request we want to report.string
- the label of your liking for this request############## gaTracker.reportLastRequestDuration(category, requestUrl, label) Not required by default
Kind: instance method of GaTracker
Summary: Manually report the duration of last sent request
duration = request initiation until last byte receipt
Access: public
Params
string
- perofmance event categorystring
- url of the request we want to report.string
- the label of your liking for this request############## gaTracker.reportLastRequestWait(category, requestUrl, label) Not required by default
Kind: instance method of GaTracker
Summary: Reports the server waiting time until download starts
Access: public
Params
string
string
string
############## gaTracker.reportLastRequestDownloadTime(category, requestUrl, label) Not required by default
Kind: instance method of GaTracker
Summary: Reports the resource download time
Access: public
Params
string
string
string
############## gaTracker.reportPage(title, page) Not required by default, if you are using 'history' package
Kind: instance method of GaTracker
Access: public
Params
string
- reported page titlestring
- page url############## gaTracker.reportException(exDescription, isFatal)
Kind: instance method of GaTracker
Summary: Reporting a code exception to GA
Access: public
Params
string
- what happenedboolean
- was the exception fatal to your code or notGaTracker
Kind: global function
Summary: Run this function as soon as possible in your code in order to initialize google analytics reporting
Returns: GaTracker
- pointer to the singleton object through which reporting is made
Access: public
Params
string
- Id of your app defined in your Google analytics account.string
- a name to represent a GA tracker.Object
- history object.RegExp
| Object
| String
- automatic performance tracking purposes. (default = /.*/)
regex string - urls to be reported should match this regex
object - type PerformanceConfig
null - performance reporting is disabled
Object
- list of google analytics field properties
Object
- list of custom dimensionsboolean
- if false [default] error / warn / log messages are sent to consoleObject
Kind: global function
Access: public
Params
string
- reported page titlestring
- the virtual pathboolean
- Object
Configuration object used in googleAnalyticsInit function
Kind: global typedef
Version: 1.1.0
Properties
string
- Only requests who's URL match this regex would be reported.[ 'Array' ].<string>
- Only requests who's type matches this strings would be reported.function
- The result of this function will be sent as category of timing eventBuilding requires node v12 and higher
Pull requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
MIT License
Copyright (c) 2018 Imperva
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
FAQs
Automatic Google Analytics reporting for your single page application, used with Redux in React, Angular, Vue
The npm package @impervaos/google-analytics-spa receives a total of 476 weekly downloads. As such, @impervaos/google-analytics-spa popularity was classified as not popular.
We found that @impervaos/google-analytics-spa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.