A lightweight JavaScript SDK for tracking payment analytics directly in the browser.
<script src="https://cdn.getsubloop.com/js/subloop-web.min.js"></script>
npm install subloop-web
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<script src="https://cdn.getsubloop.com/js/subloop-web.min.js"></script>
</head>
<body>
<script>
// Initialize SubLoop
const subloop = new SubLoopWeb('pk_your_public_key_here');
// Track a successful payment
subloop.trackPayment({
customerId: 'cust_001',
amount: 29.99,
currency: 'USD',
subscriptionId: 'sub_001',
status: 'succeeded'
});
</script>
</body>
</html>
const subloop = new SubLoopWeb(publicKey, options);
Parameters:
publicKey (string): Your SubLoop public API keyoptions (object, optional): Configuration options
baseUrl (string): API base URL (default: 'https://api.getsubloop.com')debug (boolean): Enable debug logging (default: false)subloop.trackPayment(paymentData);
Parameters:
paymentData (object): Payment information
customerId (string): Unique customer identifieramount (number): Payment amountcurrency (string): Currency code (e.g., 'USD')subscriptionId (string, optional): Subscription identifierstatus (string): Payment status ('succeeded', 'failed', 'pending')metadata (object, optional): Additional payment metadatasubloop.trackEvent(eventName, eventData);
Parameters:
eventName (string): Event name (e.g., 'subscription_created', 'trial_started')eventData (object): Event data// After successful payment processing
document.getElementById('checkout-form').addEventListener('submit', async function(e) {
e.preventDefault();
// Process payment with your payment provider
const paymentResult = await processPayment();
if (paymentResult.success) {
// Track with SubLoop
subloop.trackPayment({
customerId: paymentResult.customerId,
amount: paymentResult.amount,
currency: paymentResult.currency,
status: 'succeeded',
metadata: {
orderId: paymentResult.orderId,
paymentMethod: paymentResult.paymentMethod
}
});
// Redirect to success page
window.location.href = '/success';
}
});
// Track subscription events
function onSubscriptionCreated(subscription) {
subloop.trackEvent('subscription_created', {
customerId: subscription.customerId,
subscriptionId: subscription.id,
planId: subscription.planId,
amount: subscription.amount,
currency: subscription.currency
});
}
function onTrialStarted(trial) {
subloop.trackEvent('trial_started', {
customerId: trial.customerId,
subscriptionId: trial.subscriptionId,
trialDays: trial.days
});
}
// Get analytics data (requires authentication)
subloop.getAnalytics('overview').then(data => {
document.getElementById('mrr').textContent = `$${data.mrr}`;
document.getElementById('customers').textContent = data.activeCustomers;
});
// Development
const subloop = new SubLoopWeb('pk_test_...', {
baseUrl: 'https://api-dev.getsubloop.com',
debug: true
});
// Production
const subloop = new SubLoopWeb('pk_live_...', {
baseUrl: 'https://api.getsubloop.com',
debug: false
});
subloop.trackPayment(paymentData)
.then(response => {
console.log('Payment tracked successfully:', response);
})
.catch(error => {
console.error('Failed to track payment:', error);
// Handle error appropriately
});
pk_) for client-side trackingsk_) in client-side codeimport { useEffect } from 'react';
function PaymentSuccess({ paymentData }) {
useEffect(() => {
if (window.SubLoopWeb) {
const subloop = new window.SubLoopWeb('pk_your_key');
subloop.trackPayment(paymentData);
}
}, [paymentData]);
return <div>Payment successful!</div>;
}
<template>
<div>Payment processed</div>
</template>
<script>
export default {
mounted() {
if (window.SubLoopWeb) {
const subloop = new window.SubLoopWeb('pk_your_key');
subloop.trackPayment(this.paymentData);
}
}
}
</script>
import { Component, OnInit } from '@angular/core';
declare global {
interface Window {
SubLoopWeb: any;
}
}
@Component({
selector: 'app-payment-success',
template: '<div>Payment successful!</div>'
})
export class PaymentSuccessComponent implements OnInit {
ngOnInit() {
if (window.SubLoopWeb) {
const subloop = new window.SubLoopWeb('pk_your_key');
subloop.trackPayment(this.paymentData);
}
}
}
// Use test keys for development
const subloop = new SubLoopWeb('pk_test_your_key', {
debug: true
});
// Test payment tracking
subloop.trackPayment({
customerId: 'test_customer',
amount: 10.00,
currency: 'USD',
status: 'succeeded'
});
MIT License - see LICENSE file for details.