google-analytics-light
v1.0.6
Published
A lightweight Google Analytics tracking page views module for any JavaScript environment.
Downloads
30
Maintainers
Readme
google-analytics-light
google-analytics-light is a lightweight, standalone JavaScript library for sending Google Analytics page view events without the need for any frameworks like React, Vue, or Angular. This module allows easy integration of Google Analytics into your web applications using just plain JavaScript.
IMPORTANT
Using this plugin you can only record page views and not many advance tag manager functionalites
This function must be run in a browser environment.
Features
- Lightweight and framework-independent.
- Simple integration with Google Analytics.
- Optional Google Tag Manager (GTM) support.
- Works with any JavaScript environment (browser, Node.js, etc.).
PageSpeed Insights Issue
Installation
To install the package, run the following command:
npm install google-analytics-light
Usage
You can easily send page views to Google Analytics with this package. Here's how you can use it in your project:
// Import the function from the package
import { sendPageView } from 'google-analytics-light';
// Sending a page view when the page loads
window.onload = () => {
sendPageView('UA-XXXXXXX-X', 'GTM-XXXXX', window.location.href, 'Home Page');
};
Parameters:
- trackingId: Your Google Analytics tracking ID (e.g., UA-XXXXXXX-X).
- gtm (optional): Your Google Tag Manager (GTM) ID if you are using it (e.g., GTM-XXXXX).
- url (optional): The page URL you want to send. Defaults to window.location.href.
- title (optional): The page title. Defaults to document.title.
Example 1: Basic Usage
If you want to track page views when a user navigates to your website:
import { sendPageView } from 'google-analytics-light';
// Example: Send page view when page loads
sendPageView('UA-12345678-1');
Example 2: Custom URL and Title
You can pass custom URL and title parameters if you want to track a specific page view:
sendPageView('UA-12345678-1', '', 'https://example.com/contact', 'Contact Us Page');
Example 3: Using with GTM
If you're using Google Tag Manager (GTM), you can add the gtm parameter:
sendPageView('UA-12345678-1', 'GTM-XXXXXXX');
Author
Sarvesh Nayak