Mastering Cross-Platform Analytics: How to Log User Actions Across Web and Mobile with Amplitude
Margub Alam
GA4 & Web Analytics Specialist | Google Tag Manager | Digital Analytics Consultant | Web Analyst | Mixpanel? - Product Analytic | Amplitude Analytics| CRO | Advanced Pixel Implementation
Tracking user behavior across platforms, such as web and mobile, is essential for understanding your audience, improving your product, and driving data-driven decisions. Amplitude, a popular product analytics platform, provides the tools to log and analyze these user actions seamlessly across devices.
Why Log User Actions Across Platforms?
In today's multi-device world, users interact with applications across both web and mobile platforms. For example, a user might browse a product on their desktop and complete the purchase on their smartphone. Without unified tracking, it becomes difficult to build a clear picture of user behavior.
By logging actions across platforms, you can:
- Understand Cross-Platform Journeys: Analyze how users interact with your app across web and mobile.
- Personalize Experiences: Deliver tailored experiences based on user activity on different devices.
- Improve Retention: Identify friction points or gaps in the user journey.
- Increase Conversion Rates: Optimize the entire funnel by tracking user behavior holistically.
Key Concepts in Amplitude for Cross-Platform Tracking
Before diving into implementation, it’s important to understand key concepts in Amplitude:
- Events: Actions that users take, such as "Button Clicked," "Item Purchased," or "Page Viewed."
- Properties: Metadata attached to events, such as "Item Name," "Price," or "User Type."
- User IDs: A unique identifier that links a user across platforms.
- Device IDs: A unique identifier for a user’s device (automatically captured by Amplitude).
To unify tracking across platforms, User IDs are essential. By assigning a consistent User ID to a user, Amplitude can tie actions from both web and mobile into a single profile.
Implementation Overview
Here’s an overview of the steps to log actions across web and mobile for Amplitude:
- Integrate the Amplitude SDK: Install the appropriate Amplitude SDKs for your platforms (e.g., JavaScript for web, iOS/Android for mobile).
- Set Up Unique User IDs: Use a consistent User ID across platforms to unify tracking.
- Log Events: Send user actions (events) with relevant properties to Amplitude.
- Configure Amplitude Settings: Use Amplitude’s features like merging device IDs for anonymous users and identifying users when they log in.
Step-by-Step Guide with Code Examples
Step 1: Install the Amplitude SDKs
For Web
Install the Amplitude JavaScript SDK using npm or a CDN:
npm install @amplitude/analytics-browser
Import and initialize the SDK in your application:
import * as amplitude from '@amplitude/analytics-browser';
// Initialize Amplitude with your API key
amplitude.init('YOUR_API_KEY');
For Mobile (Android and iOS)
Add the Amplitude SDK to your mobile project.
Android (Kotlin/Java): Add the dependency to your build.gradle file:
implementation 'com.amplitude:android-sdk:2.38.0'
Initialize Amplitude in your Application class:
Amplitude.getInstance().initialize(this, "YOUR_API_KEY").enableForegroundTracking(this)
iOS (Swift): Add the SDK using CocoaPods or Swift Package Manager. For CocoaPods, update your Podfile:
pod 'Amplitude', '~> 8.0'
Initialize Amplitude in AppDelegate:
Amplitude.instance().initializeApiKey("YOUR_API_KEY")
Step 2: Set a Unique User ID
To track users across web and mobile, assign a consistent User ID once the user logs in or signs up. For anonymous users, let Amplitude handle device-level tracking.
On Web:
Set the User ID when the user logs in:
amplitude.setUserId('user_12345'); // Replace with the actual user ID
On Mobile:
Set the User ID in your mobile app:
领英推è
Android (Kotlin):
Amplitude.getInstance().setUserId("user_12345")
iOS (Swift):
Amplitude.instance().setUserId("user_12345")
Note: Ensure the same User ID is used across platforms to unify the user profile in Amplitude.
Step 3: Log Events with Properties
Log meaningful events to track user actions. Use properties to add context to events.
On Web:
Example: Track when a user views a product:
amplitude.logEvent('Product Viewed', {
product_id: 'prod_123',
product_name: 'Wireless Headphones',
price: 99.99
});
On Mobile:
Android (Kotlin):
val eventProperties = JSONObject()
eventProperties.put("product_id", "prod_123")
eventProperties.put("product_name", "Wireless Headphones")
eventProperties.put("price", 99.99)
Amplitude.getInstance().logEvent("Product Viewed", eventProperties)
iOS (Swift):
let eventProperties: [String: Any] = [
"product_id": "prod_123",
"product_name": "Wireless Headphones",
"price": 99.99
]
Amplitude.instance().logEvent("Product Viewed", withEventProperties: eventProperties)
Step 4: Merge Anonymous and Logged-In User Data
Before a user logs in, they are tracked anonymously using a device ID. Once they log in, you can merge their anonymous data with their identified user profile.
On Web:
Call amplitude.identify() when the user logs in:
amplitude.identify(new amplitude.Identify().set('user_type', 'premium'));
On Mobile:
Call the equivalent Identify method in the mobile SDK to merge data.
Step 5: Debug and Verify Events
After implementing the above steps, use Amplitude’s Debugger to verify that events are being logged correctly. You can also inspect the event payloads being sent from your app.
Best Practices for Logging Actions Across Platforms
- Use Consistent Event Names: Ensure that event names are consistent across web and mobile to simplify analysis.
- Include Relevant Properties: Add metadata to provide context to events (e.g., "Price," "Category," etc.).
- Minimize Redundant Events: Only log events that are meaningful and actionable.
- Test Across Platforms: Verify that user data is being merged correctly by simulating cross-platform usage.
- Leverage User Properties: Use user properties to capture traits like "Subscription Plan" or "Preferred Language."
Conclusion
Logging user actions across web and mobile for Amplitude is critical for gaining a comprehensive understanding of your users. By integrating the Amplitude SDKs, setting consistent User IDs, and logging events with meaningful properties, you can create a unified view of user behavior. Following the best practices outlined in this guide will help you unlock valuable insights and drive smarter product decisions.
Start implementing cross-platform tracking today to gain a deeper understanding of how your users interact with your product, no matter the device!
I’m passionate about empowering organizations with data-driven decision-making while respecting user privacy.
Here’s how you can connect with me or view my work:
Upwork Profile: Upwork
Freelancer Profile: Freelancer
My Blog on GTM & Website Analytics: Google Tag Manager Solution
If you or someone in your network is looking for an experienced professional in this space, I’d love to connect and chat further!
Understanding user behavior across platforms is indeed vital. Thank you for sharing these valuable insights on unified analytics.