|
| 1 | +## About the Braze Vega SDK |
| 2 | + |
| 3 | +The Braze Vega SDK lets you collect analytics and display rich in-app messages to your users. Most methods in the Braze Vega SDK are asynchronous and return promises that should be awaited or resolved. |
| 4 | + |
| 5 | +## Integrating the Braze Vega SDK |
| 6 | + |
| 7 | +### Step 1: Install the Braze library |
| 8 | + |
| 9 | +Install the Braze Vega SDK using your preferred package manager. |
| 10 | + |
| 11 | +{% tabs local %} |
| 12 | +{% tab npm %} |
| 13 | +If your project uses NPM, you can add the Braze Vega SDK as a dependency. |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @braze/vega-sdk --save |
| 17 | +``` |
| 18 | + |
| 19 | +After installation, you can import the methods you need: |
| 20 | + |
| 21 | +```javascript |
| 22 | +import { initialize, changeUser, openSession } from "@braze/vega-sdk"; |
| 23 | +``` |
| 24 | +{% endtab %} |
| 25 | + |
| 26 | +{% tab yarn %} |
| 27 | +If your project uses Yarn, you can add the Braze Vega SDK as a dependency. |
| 28 | + |
| 29 | +```bash |
| 30 | +yarn add @braze/vega-sdk |
| 31 | +``` |
| 32 | + |
| 33 | +After installation, you can import the methods you need: |
| 34 | + |
| 35 | +```javascript |
| 36 | +import { initialize, changeUser, openSession } from "@braze/vega-sdk"; |
| 37 | +``` |
| 38 | +{% endtab %} |
| 39 | +{% endtabs %} |
| 40 | + |
| 41 | +### Step 2: Initialize the SDK |
| 42 | + |
| 43 | +After the Braze Vega SDK is added to your project, initialize the library with the API key and [SDK endpoint URL]({{site.baseurl}}/user_guide/administrative/access_braze/sdk_endpoints) found in **Settings** > **App Settings** within your Braze dashboard. |
| 44 | + |
| 45 | +{% alert important %} |
| 46 | +You must await or resolve the `changeUser` promise before calling other Braze methods, or events and attributes may be set on the incorrect user. |
| 47 | +{% endalert %} |
| 48 | + |
| 49 | +```javascript |
| 50 | +import { useEffect } from "react-native"; |
| 51 | +import { |
| 52 | + initialize, |
| 53 | + changeUser, |
| 54 | + logCustomEvent, |
| 55 | + openSession, |
| 56 | + setCustomUserAttribute, |
| 57 | + setUserCountry |
| 58 | +} from "@braze/vega-sdk"; |
| 59 | + |
| 60 | +const App = () => { |
| 61 | + useEffect(() => { |
| 62 | + const initBraze = async () => { |
| 63 | + // Initialize the SDK |
| 64 | + await initialize("YOUR-API-KEY", "YOUR-SDK-ENDPOINT", { |
| 65 | + sessionTimeoutInSeconds: 60, |
| 66 | + appVersionNumber: "1.2.3.4", |
| 67 | + enableLogging: true, // set to `true` for debugging |
| 68 | + }); |
| 69 | + |
| 70 | + // Change user |
| 71 | + await changeUser("user-id-123"); |
| 72 | + |
| 73 | + // Start a session |
| 74 | + await openSession(); |
| 75 | + |
| 76 | + // Log custom events and set user attributes |
| 77 | + logCustomEvent("visited-page", { pageName: "home" }); |
| 78 | + setCustomUserAttribute("my-attribute", "my-attribute-value"); |
| 79 | + setUserCountry("USA"); |
| 80 | + }; |
| 81 | + |
| 82 | + initBraze(); |
| 83 | + }, []); |
| 84 | + |
| 85 | + return ( |
| 86 | + // Your app components |
| 87 | + ); |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +{% alert important %} |
| 92 | +Anonymous users may be counted towards your [MAU]({{site.baseurl}}/user_guide/data_and_analytics/reporting/understanding_your_app_usage_data/#monthly-active-users). As a result, you may want to conditionally load or initialize the SDK to exclude these users from your MAU count. |
| 93 | +{% endalert %} |
| 94 | + |
| 95 | +## Optional configurations |
| 96 | + |
| 97 | +### Logging |
| 98 | + |
| 99 | +You can enable SDK logging to help with debugging and troubleshooting. There are multiple ways to enable logging. |
| 100 | + |
| 101 | +#### Enable logging during initialization |
| 102 | + |
| 103 | +Pass `enableLogging: true` to `initialize()` to log debugging messages to the console: |
| 104 | + |
| 105 | +```javascript |
| 106 | +initialize("YOUR-API-KEY", "YOUR-SDK-ENDPOINT", { |
| 107 | + enableLogging: true |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +{% alert important %} |
| 112 | +Basic logs are visible to all users, so consider disabling logging before releasing your code to production. |
| 113 | +{% endalert %} |
| 114 | + |
| 115 | +#### Enable logging after initialization |
| 116 | + |
| 117 | +Use `toggleLogging()` to enable or disable SDK logging after initialization: |
| 118 | + |
| 119 | +```javascript |
| 120 | +import { toggleLogging } from "@braze/vega-sdk"; |
| 121 | + |
| 122 | +// Enable logging |
| 123 | +toggleLogging(); |
| 124 | +``` |
| 125 | + |
| 126 | +#### Custom logging |
| 127 | + |
| 128 | +Use `setLogger()` to provide a custom logger function for more control over how SDK logs are handled: |
| 129 | + |
| 130 | +```javascript |
| 131 | +import { setLogger } from "@braze/vega-sdk"; |
| 132 | + |
| 133 | +setLogger((message) => { |
| 134 | + console.log("Braze Custom Logger: " + message); |
| 135 | + // Add your custom logging logic here |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +### Configuration options |
| 140 | + |
| 141 | +You can pass additional configuration options to `initialize()` to customize the SDK behavior: |
| 142 | + |
| 143 | +```javascript |
| 144 | +await initialize("YOUR-API-KEY", "YOUR-SDK-ENDPOINT", { |
| 145 | + sessionTimeoutInSeconds: 60, // Configure session timeout (default is 30 seconds) |
| 146 | + appVersionNumber: "1.2.3.4", // Set your app version |
| 147 | + enableLogging: true, // Enable SDK logging |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +## Upgrading the SDK |
| 152 | + |
| 153 | +When you reference the Braze Vega SDK from NPM or Yarn, you can upgrade to the latest version by updating your package dependency: |
| 154 | + |
| 155 | +```bash |
| 156 | +npm update @braze/vega-sdk |
| 157 | +# or, using yarn: |
| 158 | +yarn upgrade @braze/vega-sdk |
| 159 | +``` |
| 160 | + |
| 161 | +## Testing your integration |
| 162 | + |
| 163 | +To verify your SDK integration is working correctly: |
| 164 | + |
| 165 | +1. Initialize the SDK with `enableLogging: true` to see debug messages in the console |
| 166 | +2. Ensure you `await changeUser()` before calling other SDK methods |
| 167 | +3. Call `await openSession()` to start a session |
| 168 | +4. Check your Braze dashboard under **Overview** to verify that session data is being recorded |
| 169 | +5. Test logging a custom event and verify it appears in your dashboard |
| 170 | + |
| 171 | + |
0 commit comments