Events Integration

Track user actions with Weespin (logEvent, setUserId, initializeDevice)

Track user behavior and analytics in your Flutter application with Weespin's event tracking system to measure conversions, analyze engagement, and optimize your campaigns.

Thanks to the Weespin package, you can:

  • Track custom events (purchases, sign-ups, add to cart).
  • Associate events with users and generated links.
  • Collect valuable data to improve your product.

Prerequisites: Get your API keys

  1. Log into your Weespin dashboard.
  2. Go to your application.
  3. Go to the Configuration section.
  4. In Integration:
    • Generate the API key that will be used to identify your application.

Integrate the Weespin Flutter plugin

  1. Add the dependency in pubspec.yaml:
    dependencies:
      weespin_flutter: ^1.0.0
    
  2. Import the package in your Dart code:
    import 'package:weespin_flutter/weespin_flutter.dart';
    

Initialize the SDK

Before sending events, initialize the Weespin client and device information:

final weespin = Weespin(
  apiKey: 'YOUR_API_KEY',
  userId: 'user_12345', // optional but recommended
);

@override
void initState() {
  super.initState();
  weespin.initializeDevice();
}

Send a custom event

Use the logEvent method to track user actions with custom data:

Future<void> sendLogEvent() async {
  try {
    final result = await weespin.logEvent(
      eventName: "add_to_cart",
      payload: {
        "product_id": "prd_00109",
        "quantity": 2,
        "price": 49.99,
        "screen": "home",
        "platform": "android",
      },
    );

    print("Event sent: ${result.eventName}");
  } catch (e) {
    print("Error sending event: $e");
  }
}

You can then trigger this from any UI action:

ElevatedButton(
  onPressed: sendLogEvent,
  child: const Text('Add to Cart'),
)

Update user identifier

You can change the user ID at any time, useful after login or signup:

weespin.setUserId('user_67890');

Event payload rules

Requirements:

  • eventName must be a non-empty string.
  • payload must be a JSON-compatible map.

Supported types:

  • String
  • int
  • double
  • bool

Example payload:

{
  "order_id": "ORD_123",
  "amount": 129.99,
  "currency": "EUR",
  "payment_method": "card",
}

Common event names:

  • add_to_cart - User adds a product to cart
  • purchase - User completes a transaction
  • signup - New user registration
  • login - User authentication
  • share_link - User shares content
  • open_app - App launch

Example usage in Flutter

main.dart
import 'package:flutter/material.dart';
import 'package:weespin_flutter/weespin_flutter.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late Weespin weespin;

  @override
  void initState() {
    super.initState();
    
    weespin = Weespin(
      apiKey: 'YOUR_API_KEY',
      userId: 'user_12345',
    );
    
    weespin.initializeDevice();
  }

  Future<void> trackPurchase() async {
    try {
      await weespin.logEvent(
        eventName: "purchase",
        payload: {
          "order_id": "ORD_${DateTime.now().millisecondsSinceEpoch}",
          "amount": 99.99,
          "currency": "USD",
          "items": 3,
        },
      );
      
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Purchase tracked!')),
        );
      }
    } catch (e) {
      print("Error: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Weespin Events')),
        body: Center(
          child: ElevatedButton(
            onPressed: trackPurchase,
            child: const Text('Complete Purchase'),
          ),
        ),
      ),
    );
  }
}

Test your events

Dashboard: Check your Weespin dashboard to see events in real-time.

// Enable debug mode (do not use in production)
await weespin.logEvent(
  eventName: "test_event",
  payload: {"debug": true},
);

Conclusion / Best Practices

By integrating event tracking with Weespin:

  • You gain valuable insights into user behavior.
  • You can measure the effectiveness of your features.
  • You optimize conversion funnels based on real data.