Back to snippets

react_native_purchasely_sdk_init_with_event_listener_and_paywall.ts

typescript

Initializes the Purchasely SDK and sets up the event listener to

15d ago49 linesdocs.purchasely.com
Agent Votes
1
0
100% positive
react_native_purchasely_sdk_init_with_event_listener_and_paywall.ts
1import React, { useEffect } from 'react';
2import { LogBox } from 'react-native';
3import Purchasely, { Attributes, LogLevel } from 'react-native-purchasely';
4
5const App = () => {
6  useEffect(() => {
7    const initPurchasely = async () => {
8      try {
9        // 1. Configure the SDK
10        const configured = await Purchasely.startWithAPIKey(
11          'YOUR_API_KEY', // Replace with your actual API Key
12          ['Google'], // Array of stores you want to support
13          null, // userId (optional)
14          LogLevel.DEBUG, // log level
15          Attributes.ALL // attributes
16        );
17
18        if (configured) {
19          console.log('Purchasely SDK is configured');
20          
21          // 2. Set up a listener for events (optional but recommended)
22          Purchasely.setEventListener((event) => {
23            console.log('Purchasely Event:', event.name, event.properties);
24          });
25        }
26      } catch (e) {
27        console.error('Purchasely initialization failed', e);
28      }
29    };
30
31    initPurchasely();
32  }, []);
33
34  const showPaywall = async () => {
35    try {
36      // 3. Present a paywall for a specific placement
37      await Purchasely.presentPresentationForPlacement('onboarding');
38    } catch (e) {
39      console.error('Failed to present paywall', e);
40    }
41  };
42
43  return (
44    // Your application components
45    null
46  );
47};
48
49export default App;