{"id":23666634,"url":"https://github.com/redsift/js-lib-google-analytics","last_synced_at":"2026-02-03T03:06:16.466Z","repository":{"id":145311442,"uuid":"155986809","full_name":"redsift/js-lib-google-analytics","owner":"redsift","description":"Setup Google Analytics with cross-linked projects and more.","archived":false,"fork":false,"pushed_at":"2024-02-03T06:01:50.000Z","size":209,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-06-30T01:38:14.407Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redsift.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-03T13:43:05.000Z","updated_at":"2018-11-08T11:38:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"78e7cad0-ad47-49ce-8f53-b4bc18f0f6af","html_url":"https://github.com/redsift/js-lib-google-analytics","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/redsift/js-lib-google-analytics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsift%2Fjs-lib-google-analytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsift%2Fjs-lib-google-analytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsift%2Fjs-lib-google-analytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsift%2Fjs-lib-google-analytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redsift","download_url":"https://codeload.github.com/redsift/js-lib-google-analytics/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsift%2Fjs-lib-google-analytics/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29029771,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T02:28:16.591Z","status":"ssl_error","status_checked_at":"2026-02-03T02:27:48.904Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-29T07:33:21.791Z","updated_at":"2026-02-03T03:06:16.427Z","avatar_url":"https://github.com/redsift.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js-lib-google-analytics\n\n`js-lib-google-analytics` is a helper library to setup Google Analytics via [analytics.js](https://developers.google.com/analytics/devguides/collection/analyticsjs/). It provides:\n\n* Setup for sending of events to a single or multiple Google Analytics projects.\n* Setup of [cross domain linking](https://support.google.com/analytics/answer/1034342?hl=en).\n* Allows to enable [IP anonimization](https://developers.google.com/analytics/devguides/collection/analyticsjs/ip-anonymization).\n* Optional configuration for the `autotrack.js` plugins `cleanUrlTracker`, `pageVisibilityTracker` and `urlChangeTracker`.\n\n## Setup\n\n### Prerequisites: analytics.js and (optionally) autotrack.js\n\n[analytics.js](https://developers.google.com/analytics/devguides/collection/analyticsjs/) is Google's official tracking library, providing the global `ga` object which this repository helps to configure. Please use [the installation instructions](https://developers.google.com/analytics/devguides/collection/analyticsjs/) to setup `analytics.js`.\n\nOptionally you can install the [autotrack.js](https://github.com/googleanalytics/autotrack) library, which, according to its Github repository, does *\"Automatic and enhanced Google Analytics tracking for common user interactions on the web.\"*. Check the repository documentation for more information and see the next step on how to configure a selection of `autotrack` plugins via `js-lib-google-analytics`.\n\n### Usage\n\n### Default usage\n\nTo use the default configuration which comes with the library use the following code:\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';\n\nconst config = {\n    uaProjectId: 'UA-PROJECT-ID',\n    ...getDefaultProjectSetup(),\n};\n\nsetupGoogleAnalytics(config);\n```\n\nUsing `getDefaultProjectSetup()` yields this configuration:\n\n```javascript\n{\n    anonymizeIp: true, // enable IP anonimization\n    cookieName: '_ga',\n    anonymizeIp: true,\n    userId: null, // if set the `userId` will be set for the tracker, see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#userId\n    userId: null, // if set the `userId` will be set for the tracker, see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en#clientId\n    clientId: null,\n    autoLink: [], // don't configure cross domain linking \n    sendInitialPageView: true, // send an initial page view when using `setupGoogleAnalytics()`\n    autoTrack: {\n      // configure the `cleanUrlTracker` autotrack plugin with this configuration\n      // see https://github.com/googleanalytics/autotrack/blob/master/docs/plugins/clean-url-tracker.md        \n      cleanUrlTracker: {\n        stripQuery: true,\n        queryDimensionIndex: 1,\n        indexFilename: 'index.html',\n        trailingSlash: 'remove',\n      },\n      // enable the `urlChangeTracker` autotrack plugin\n      // see https://github.com/googleanalytics/autotrack/blob/master/docs/plugins/url-change-tracker.md#differentiating-between-virtual-pageviews-and-the-initial-pageview\n      urlChangeTracker: true, // configure the `urlChangeTracker` autotrack plugin with this configuration\n    },\n}\n```\n\nYou can use this configuration as a base for your own customization.\n\n\u003e NOTE: If you don't provide a `name` property to the config the tracker name will be derived from the `uaProjectId` in removing all `-`.\n\n### Sending events to multiple projects\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';\n\nconst configs = [{\n    uaProjectId: 'UA-PROJECT-ID-0',\n    ...getDefaultProjectSetup(),\n}, {\n    uaProjectId: 'UA-PROJECT-ID-1',\n    ...getDefaultProjectSetup(),\n}];\n\nsetupGoogleAnalytics(configs);\n```\n\nThis will send events to each configured projects. A use case is to have a GA project which only collects events for a single domain, and a second project which collects events from all your domains, including the one you are using `@redsift/js-lib-google-analytics` for (in this case it is useful to setup cross domain linking, see below).\n\n### Cross domain linking\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';\n\nconst config = {\n    uaProjectId: 'UA-PROJECT-ID',\n    ...getDefaultProjectSetup(),\n    autoLink: ['blog.myproject.at', 'docs.myproject.at'],\n};\n\nsetupGoogleAnalytics(config);\n```\n\nThis will provide pageview and URL information for the domains referenced in `autoLink` in the `UA-PROJECT-ID` project.\n\n### Usage without `autotrack.js`\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';\n\nconst config = {\n    uaProjectId: 'UA-PROJECT-ID',\n    ...getDefaultProjectSetup(),\n    autoTrack: null,\n};\n\nsetupGoogleAnalytics(config);\n```\n\nThis will provide setup `analytics.js` but will skip the configuration of `autotrack.js`.\n\n### Trigger events for all configured projects\n\nAfter you called `setupGoogleAnalytics()` you can send events to all of them with a single command. Please note that this command only supportes 3 parameters max:\n\n```javascript\nimport setupGoogleAnalytics, { gaAll } from '@redsift/js-lib-google-analytics';\n\n// ... setup you projects\n\ngaAll('send', 'pageview'); // Sends a pageview event to all configured projects.\n```\n\n### Configure a session based cookie (which immediately expires)\n\nSee https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#cookie_expiration for more information.\n\nTo create a permanent cookie after initializing the session based on you can call `setupGoogleAnalytics()` without the `temporarySession` config parameter. The `clientId` for the cookie will stay the same in this case, so your GA project will treat the two sessions as the same user.\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup } from '@redsift/js-lib-google-analytics';\n\nconst temporarySessionConfig = {\n    uaProjectId: 'UA-PROJECT-ID',\n    ...getDefaultProjectSetup(),\n    temporarySession: true,\n};\n\nsetupGoogleAnalytics(temporarySessionConfig);\n\n// To make the cookie permanent call the setup function again without `temporarySession`:\n\nconst config = {\n    uaProjectId: 'UA-PROJECT-ID',\n    ...getDefaultProjectSetup(),\n};\n\nsetupGoogleAnalytics(config);\n```\n\n### Get a list of the configured project names\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup, getProjectNames } from '@redsift/js-lib-google-analytics';\n\nconst config = [{\n    uaProjectId: 'UA-PROJECT-ID-0',\n    ...getDefaultProjectSetup(),\n}, {\n    uaProjectId: 'UA-PROJECT-ID-1',\n    ...getDefaultProjectSetup(),\n}];\n\nsetupGoogleAnalytics(config);\n\nconsole.log(getProjectNames()); // Returns a Set of project names, here: ['UA-PROJECT-ID-0' 'UA-PROJECT-ID-1']\n\nconsole.log(getProjectNames({ asArray: true })); // Returns an Array of project names, here: ['UA-PROJECT-ID-0' 'UA-PROJECT-ID-1']\n```\n\n### Set a custom project name for the tracker\n\n```javascript\nimport setupGoogleAnalytics, { getDefaultProjectSetup, getProjectNames } from '@redsift/js-lib-google-analytics';\n\nconst config = {\n    uaProjectId: 'UA-PROJECT-ID-0',\n    name: 'MyTracker',\n    ...getDefaultProjectSetup(),\n};\n\nsetupGoogleAnalytics(config);\n\nconsole.log(getProjectNames()); // ['MyTracker']\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredsift%2Fjs-lib-google-analytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredsift%2Fjs-lib-google-analytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredsift%2Fjs-lib-google-analytics/lists"}