What is @amplitude/analytics-node?
@amplitude/analytics-node is an npm package that allows you to integrate Amplitude's analytics capabilities into your Node.js applications. It provides functionalities for tracking events, identifying users, and setting user properties, which can help you understand user behavior and improve your product.
What are @amplitude/analytics-node's main functionalities?
Track Events
This feature allows you to track events that occur in your application. The code sample demonstrates how to log a 'user_signup' event with associated properties.
const amplitude = require('@amplitude/analytics-node');
const client = amplitude.init('YOUR_API_KEY');
client.logEvent({
event_type: 'user_signup',
user_id: 'user123',
event_properties: {
plan: 'premium'
}
});
Identify Users
This feature allows you to identify users and set their properties. The code sample shows how to identify a user and set their email and name.
const amplitude = require('@amplitude/analytics-node');
const client = amplitude.init('YOUR_API_KEY');
client.identify({
user_id: 'user123',
user_properties: {
$set: {
email: 'user@example.com',
name: 'John Doe'
}
}
});
Set User Properties
This feature allows you to set or update properties for a user. The code sample demonstrates how to set the age and gender properties for a user.
const amplitude = require('@amplitude/analytics-node');
const client = amplitude.init('YOUR_API_KEY');
client.identify({
user_id: 'user123',
user_properties: {
$set: {
age: 30,
gender: 'male'
}
}
});
Other packages similar to @amplitude/analytics-node
mixpanel
Mixpanel is a powerful analytics tool that helps you track user interactions with web and mobile applications. It offers similar functionalities to @amplitude/analytics-node, such as event tracking, user identification, and setting user properties. Mixpanel also provides advanced features like A/B testing and user segmentation.
segment
Segment is a customer data platform that allows you to collect, clean, and control your customer data. It provides functionalities for tracking events, identifying users, and setting user properties, similar to @amplitude/analytics-node. Segment also integrates with a wide range of other analytics and marketing tools, making it a versatile choice for managing customer data.
heap
Heap is an analytics platform that automatically captures all user interactions with your web and mobile applications. It offers functionalities similar to @amplitude/analytics-node, such as event tracking and user identification. Heap's unique selling point is its ability to retroactively analyze data without requiring manual event tracking.
@amplitude/analytics-node
Official Amplitude SDK for Node.js
Installation
To get started with using Amplitude Node.js SDK, install the package to your project via NPM.
Using Node package
This package is published on NPM registry and is available to be installed using npm and yarn.
npm install @amplitude/analytics-node
yarn add @amplitude/analytics-node
Usage
Initializing SDK
Initialization is necessary before any instrumentation is done. The API key for your Amplitude project is required.
amplitude.init(API_KEY);
Tracking an Event
Events represent how users interact with your application. For example, "Button Clicked" may be an action you want to note.
import { track } from '@amplitude/analytics-node';
track('Button Clicked');
const eventProperties = {
selectedColors: ['red', 'blue'],
};
track('Button Clicked', eventProperties);
User Properties
User properties help you understand your users at the time they performed some action within your app such as their device details, their preferences, or language.
import { Identify, identify } from '@amplitude/analytics-node';
const event = new Identify();
event.set('key1', 'value1');
event.setOnce('key1', 'value1');
event.add('value1', 10);
event.preInsert('ab-tests', 'new-user-test');
event.postInsert('ab-tests', 'new-user-test');
event.remove('ab-tests', 'new-user-test')
identify(event);
prepend/append
- append will append a value or values to a user property array.
- prepend will prepend a value or values to a user property.
User Groups
import { setGroup } from '@amplitude/analytics-node';
setGroup('orgId', '15');
setGroup('sport', ['soccer', 'tennis']);
Group Identify
This feature is only available to Growth and Enterprise customers who have purchased the Accounts add-on.
Use the Group Identify API to set or update properties of particular groups. However, these updates will only affect events going forward.
import { Identify, groupIdentify } from '@amplitude/analytics-node';
const groupType = 'plan';
const groupName = 'enterprise';
const identity = new Identify()
identity.set('key1', 'value1');
groupIdentify(groupType, groupName, identity);
Track Revenue
Revenue instances will store each revenue transaction and allow you to define several special revenue properties (such as 'revenueType', 'productIdentifier', etc.) that are used in Amplitude's Event Segmentation and Revenue LTV charts. These Revenue instance objects are then passed into revenue
to send as revenue events to Amplitude. This allows us to automatically display data relevant to revenue in the platform. You can use this to track both in-app and non-in-app purchases.
import { Revenue, revenue } from '@amplitude/analytics-node';
const event = new Revenue()
.setProductId('com.company.productId')
.setPrice(3.99)
.setQuantity(3);
revenue(event);
Callback
All asynchronous API are optionally awaitable through a specific Promise interface. This also serves as callback interface.
const results = await track('Button Clicked').promise;
result.event;
result.code;
result.message;
track('Button Clicked').promise.then((result) => {
result.event;
result.code;
result.message;
});