What is @amplitude/analytics-browser?
@amplitude/analytics-browser is an npm package that allows you to integrate Amplitude's analytics capabilities into your web applications. It provides tools for tracking user events, managing user identities, and setting up custom properties to gain insights into user behavior.
What are @amplitude/analytics-browser's main functionalities?
Initialize Amplitude
This feature allows you to initialize the Amplitude analytics instance with your API key, which is necessary to start tracking events.
const amplitude = require('@amplitude/analytics-browser');
amplitude.init('YOUR_API_KEY');
Track Events
This feature allows you to track specific events within your application. In this example, a 'Button Clicked' event is tracked with an additional property 'buttonName'.
amplitude.track('Button Clicked', { buttonName: 'Sign Up' });
Identify Users
This feature allows you to identify users and set user properties. In this example, a user with ID 'user123' is identified and a custom property 'plan' is set to 'premium'.
amplitude.identify({ userId: 'user123', userProperties: { plan: 'premium' } });
Set User Properties
This feature allows you to set custom properties for a user. In this example, the user's age and gender are set.
amplitude.setUserProperties({ age: 30, gender: 'female' });
Log Revenue
This feature allows you to log revenue events. In this example, a revenue event is logged with an amount of 10.99 for product 'product_123' with a quantity of 1.
amplitude.logRevenue(10.99, 'product_123', 1);
Other packages similar to @amplitude/analytics-browser
mixpanel-browser
Mixpanel is another popular analytics tool that provides similar functionalities to Amplitude. It allows you to track user interactions, manage user identities, and set custom properties. Mixpanel also offers advanced features like A/B testing and user segmentation.
google-analytics
Google Analytics is a widely-used web analytics service that tracks and reports website traffic. While it offers extensive tracking and reporting capabilities, it is more focused on web traffic analysis compared to the user-centric approach of Amplitude.
@amplitude/analytics-browser
Installation
To get started with using Amplitude Browser SDK, install the package to your project via NPM or script loader.
Using Node package
This package is published on NPM registry and is available to be installed using npm and yarn.
npm install @amplitude/analytics-browser
yarn add @amplitude/analytics-browser
Usage
Initializing SDK
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-browser';
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-browser';
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-browser';
// set group with single group name
setGroup('orgId', '15');
// set group with multiple group names
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-browser';
const groupType = 'plan';
const groupName = 'enterprise';
const event = new Identify()
event.set('key1', 'value1');
groupIdentify(groupType, groupName, identify);
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-browser';
const event = new Revenue()
.setProductId('com.company.productId')
.setPrice(3.99)
.setQuantity(3);
revenue(event);