Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM
Accelerate development with AI

On this page
Looking for more inspiration?Visit the

Single Page Apps with Analytics.js


Analytics.js can work with your single-page applications built with technologies such as React or Vue.


Integrating with your build tooling

integrating-with-your-build-tooling page anchor

If you have a single-page application and plan to use Segment a lot, you might need to add a Segment server analytics library to your build system. You can do this using Segment's analytics-node package instead of using the Analytics.js javascript code snippet. The Analytics-Node package works like any standard node-js dependency.

First add the package as a dependency:

$ npm install analytics-node

or, if you are using yarn:

$ yarn add analytics-node

Once installed, you can import analytics-node and instrument your events the same way you would with Analytics.js. The example implementation below is in React:

1
import Analytics from 'analytics-node'
2
3
const analytics = new Analytics("YOUR WRITE KEY")
4
5
class ShoppingCartButton {
6
onPurchase = () => {
7
analytics.track("Item Purchased", {
8
itemId: "abc"
9
})
10
}
11
12
render() {
13
return <button onClick={this.onPurchase}>Buy Item</button>
14
}
15
16
}