English · IndiaLog in
Rackwave Technologies
Request quote Free consultation
DataUpdated 26 Nov 2026

Segment Analytics.js: Web Tracking Setup Guide

Segment Analytics.js: installation, core calls, single page apps, consent and debugging.

Written by Arjun Mehta
  • Published24 Nov 2026
  • Read time6 min
  • Length1,309 words
Share this guide

What is Segment Analytics.js? Segment Analytics.js is the JavaScript library that collects data from websites and sends it to Segment, using a small set of calls: identify for users, track for actions, page for page views and group for accounts. Once the snippet or package is installed, the library loads, creates an anonymous identifier, queues events and delivers them to Segment, which then routes them to destinations in cloud mode or loads other tools in device mode. This guide explains how Analytics.js works, how to install it, how to use the core calls, how to handle identity, consent and single page applications, how to test and debug, and which mistakes cause missing events or double counting. Capabilities change, so confirm details in current Segment documentation.

Short answer: Analytics.js is the web library for Segment. Install it once, call identify, track and page according to your tracking plan, handle consent and test in the debugger.

What is Segment Analytics.js?

It is the main web source for what is Segment and implements the events defined in your tracking plan. It feeds destinations and builds the identity data used by Unify profiles. For web context, see what is an API.

How the library works

Diagram of Analytics.js: website page loads the library, anonymous ID is created, calls queue events, events are sent to Segment and routed to destinations
Figure 1. The page loads the library, which creates an anonymous ID and sends identify, track and page calls to Segment for routing.
CallPurposeExample
analytics.pageRecords a page viewPricing page
analytics.identifyLinks a user ID and traitsUser signs in
analytics.trackRecords an action with propertiesOrder Completed
analytics.groupLinks a user to an accountCompany and plan
analytics.resetClears the current user on logoutSign out
analytics.readyRuns code after destinations loadDevice mode tools

Installation options

  • Add the snippet to the head of every page, ideally through a tag manager.
  • Or install the npm package in a modern application.
  • Use the write key for the correct source and environment.
  • Load the library early so that early events are not lost.
  • Keep separate sources for production and testing.
  • Review the snippet after each major version change.
TopicGuidance
Page viewsCall page on each route change, not only on first load
IdentityCall identify after login and reset on logout
ConsentLoad or enable destinations only after the user agrees to the matching category
CookiesDocument first party cookies used by the library
BlockersExpect some traffic to be blocked and plan server side events for key data
PerformanceLoad asynchronously and avoid heavy device mode tools

How to implement and verify

  1. Plan. Use the tracking plan to list calls and properties.
  2. Install. Add the snippet or package with the right write key.
  3. Add calls. Implement page, identify and track calls.
  4. Handle consent. Connect the library to your consent tool.
  5. Debug. Use the debugger and browser tools to check events.
  6. Compare. Check counts against other analytics for a few days.
  7. Release and monitor. Watch volumes and violations after launch.

Common mistakes

  • Calling page only once in a single page app.
  • Forgetting to call reset on logout.
  • Sending personal data in event properties.
  • Installing the snippet twice.
  • Using production keys in testing.
  • Relying on client side events for revenue.

Our API development service builds server side tracking and our data analytics service validates tracking plans.

A worked example: a content and pricing website

A software company installs Analytics.js through a tag manager and sends page calls on every route change. When a visitor submits a demo form, the site calls identify with the email and name, and then track with Demo Requested and properties for source and page. After login, identify uses the stable user ID.

The consent tool controls which destinations load, so advertising tools receive data only after agreement. The team compares counts with another analytics tool for a week and finds that a single page route did not call page, which is fixed. Server side events send Order Completed, so revenue figures stay reliable even when browsers block scripts.

Identity handling in the browser

The library creates an anonymous ID stored in a first party cookie or local storage, and links it to a user ID when identify is called. Decide what happens when the same browser is used by several people, and call reset when a user logs out so that the next visitor is not tied to the previous person. Pass the same user ID from the server where possible, and test the full journey from anonymous visit to sign in, to make sure history joins correctly.

Performance and page speed

Analytics code should not slow down your site. Load the library asynchronously, keep device mode tools to the minimum and prefer cloud mode where you can. Measure the effect on page load and interaction using standard performance tools, and review the cost of each added destination. Fast pages are better for users and for search, and a lean tracking set makes debugging much easier.

Privacy and cookies

Analytics.js uses identifiers that may be personal data. Describe the cookies and storage in your policy, provide choices through a consent tool and respect them in the library configuration. Avoid placing personal data in page URLs or event properties, and limit traits to what is needed. Provide a way to handle deletion requests across Segment, destinations and the warehouse. See what is GDPR for principles and review settings with legal advisers.

Testing and debugging

Use the Segment debugger and browser developer tools to watch events leave the page and arrive in the source. Check names, types and required properties against the tracking plan, and test on several browsers and devices. Include logged in and logged out journeys, and test with and without consent. Keep a test script for key flows and repeat it after each release, so that changes to the site do not quietly break tracking.

Maintenance and governance

Name an owner for the web implementation, and keep a record of the library version, write keys and installed destinations. Review the implementation when the site is redesigned or migrated, since new templates often drop tracking. Use release checks that include analytics, and tie changes to the tracking plan process. Regular care keeps web data complete and consistent over time.

Working with tag managers

Many teams install Analytics.js through a tag manager so that marketers can control tags without a code release. This is convenient, but it needs discipline. Restrict who can publish changes, use a preview environment, and name tags clearly. Avoid sending the same event from both the tag manager and the application code, which causes double counting. Document which events come from which place, and review the container regularly to remove old tags.

Migrating from other web analytics tools

When moving from separate tags to Analytics.js, run both for a period and compare counts, because differences in definitions are common. Map existing event names to the new plan, remove tags that duplicate destinations and tell teams which reports will change. Plan the cut over for a quiet period, and keep a rollback option. A careful migration protects historical comparisons and builds trust in the new data stream.

Key takeaways

  • Analytics.js is the Segment JavaScript library for collecting website data.
  • Use page, identify, track and group calls according to the tracking plan.
  • Call page on every route change in single page apps and reset on logout.
  • Connect the library to consent tools and keep personal data out of events.
  • Test in the debugger and compare counts with other tools.

Quick glossary

TermMeaning
Analytics.jsThe Segment JavaScript library for websites
Write keyThe key that links the library to a Segment source
Anonymous IDAn identifier created for an unknown visitor
Device modeLoading destination tools directly in the browser
ResetA call that clears the current user identity

Read next: BigQuery vs Snowflake: Which Cloud Data Platform? · BigQuery Streaming Inserts: Real-Time Ingestion Guide · BigQuery Access Control: IAM, Policy Tags and Row Security

Frequently asked questions

What is Analytics.js?

It is the Segment JavaScript library that collects website data and sends it to Segment.

Which calls should I use?

Page for views, identify for users, track for actions and group for accounts.

How do I handle single page apps?

Call page on each route change.

When should I call reset?

On logout, so the next visitor is not tied to the previous user.

How do I handle consent?

Connect the library to your consent tool and enable destinations only for approved categories.

How do I debug?

Use the Segment debugger and browser developer tools.

Why are some events missing?

Common causes are blockers, late loading, duplicate snippets and missing calls on some routes.

Should I track revenue on the client?

Prefer server side events for revenue because they are more reliable.

Was this guide helpful?Your answer helps us decide what to improve. It is not linked to you.

#segment analytics.js#analytics.js segment#segment web tracking#segment javascript library#segment page identify track

About the author

Arjun Mehta

Integration Architect

Arjun designs and builds integration architecture connecting CRM, ERP, and custom systems, with a specific focus on API-led connectivity patterns using MuleSoft.

Integration & APIs (MuleSoftREST/Bulk APIs)

Ready to put AI and data to work?

Book a free consultation and leave with a plan, not a pitch.

Get a free consultationRequest quote