React Native
Welcome to the comprehensive guide on integrating the MomentScience Moments API into your React Native application! This guide will provide you with step-by-step instructions to seamlessly incorporate MomentScience Offers into your app's user experience.
Before diving into the integration process, there are a few preliminary steps you need to take
To access the Moments API, you must obtain an API key. Follow the instructions provided to acquire your unique API key.
- Determine where MomentScience Offers will display within your app.
- Decide whether Offers will appear as part of a screen or as full-screen modal overlays.
- Specify whether Offers should display once per session or each time a specific section of the app is accessed.
Let's kickstart the integration process by completing the following tasks:
You'll need to install the necessary dependency packages, Axios and react-native-device-info, to make API calls from your app and obtain device-related information. Run the provided command to install these packages.
To ensure offer links open in an external browser on Android devices, add the provided intent filter to the AndroidManifest.xml file of your app.
When calling our API, it's important to include the User-Agent parameter. Here's how you can retrieve its value for the device using the react-native-device-info package.
When communicating with the API, it's recommended to include an identifier unique to each user. In the code example provided, we utilize the Device ID retrieved from the 'react-native-device-info' package for this purpose. To implement this, simply pass an alphanumeric string unique to each user on your system. to the adpx_fp parameter within the payload of the Fetch Offers call.
MomentScience uses the adpx_fp value to control how often offers are shown to a user and to prevent the same offer from appearing again when a user opts out.
Utilize axios to retrieve offers from the Moments API. Structure the API call with relevant parameters such as apiKey, query, and payload.
The function below is a "wrapper" for the Moments API. For complete information about the Moments API, please refer to the Moments API documentation.
Now you can call the fetchData function anywhere in your app whenever you need to retrieve offer data.
When dev is set to '1', the sandbox environment is enabled for testing purposes only, with no activity recorded in production. In this mode, geo-targeting is ignored for publishers, ensuring that all offers are returned in ad responses for comprehensive testing.
In the previous code snippet, several functions are invoked to facilitate various operations. Here's an overview of each function and its purpose:
- firePixelFunction: This function is utilized to send requests to different URLs based on specific conditions:
- When a new offer is displayed on the screen.
- When a negative Call-To-Action (CTA) like "No thanks" is tapped.
- When the user taps on the Close button.
- openUrlFunction: This function is invoked when the user interacts positively with the offer image or a Call-To-Action (CTA). It facilitates opening a link in an external browser for further engagement.
After obtaining the data in the previous step, it's time to design the user interface (UI) and update it according to the data. We'll also want to ensure that different event beacons are fired as the user engages with the UI.
In this example, we'll showcase a standard modal box that displays a sequence of offers with navigation and a close button.
While this example features a modal/pop-up box, feel free to build any experience you wish. The primary purpose of this example is to demonstrate navigation between offers as well as how to fire event beacons as the user moves along the offer sequence.
We'll split the UI into two main parts.
This section encompasses the navigation buttons, offer display area, and a close button.
- Navigation Buttons: These allow users to move to the previous or next offer.
- Close Button: Enables users to close the entire Offer Container UI.
- Input Data: Data required to render the Offer UI inside the Offer Container UI.
We'll set up a callback function:
OnCloseOfferCTA: we send a request to the URL defined in beacons?.close is invoked when the user closes an OfferContainerView OR when the user taps on the negative CTA on the last offer presented. When invoked by tapping on the close button, shouldFirePixel will be set to true. When invoked by tapping on the negative CTA on the last offer presented, shouldFirePixel will be set to false.
Once the user closes the Offer Container UI, the next time you want to show offers, you need to reload them. To reload offers, you can reset the offer state and fetch new data. Here's how you can do it:
Item | Description |
---|---|
currentOffer | Keeps track of the current offer displayed by the OfferView. |
currentOfferIndex | Tracks the position of the currently displayed offer in an array of offers. |
openURL | Function called when the user taps on the offer image or a positive call-to-action (CTA). |
firePixel | Function to send requests to various URLs under different conditions, including rendering new offers.
|
goToNextOffer | responsible for handling actions when want to display next offer. |
goToPreviousOffer | responsible for handling actions when want to display prev offer. |
OnCloseOfferCTA | callback function which will be invoked when user tap on close button and when last offer has been viewed. |
Complete Source Code of OfferContainerView.js
The OfferView component is crucial for rendering individual offer units and managing various interactions. The primary responsibility of OfferView is to render an individual offer unit.
Each OfferView has the following parts:
Offer Part | Description |
---|---|
title | Represents the title of the offer. .title |
imageURL | Represents the URL of the offer image. .image |
description | Represents the description of the offer. .description |
clickURL | Represents the URL to hit when the positive CTA is tapped or when the offer image is tapped. |
onImageCTA | Represents a callback function triggered when the offer image is tapped.
|
positiveCTA | Represents the text for the positive call-to-action (CTA). .cta_yes |
onPositiveCTA | Represents a callback function triggered when the positive CTA is tapped. In this callback function:
|
negativeCTA | Represents the text for the negative CTA. .cta_no |
onNegativeCTA | Represents a callback function triggered when the negative CTA is tapped. In this callback function:
|
Explore the MomentScience Example on GitHub by visiting the provided link. Download the example, then run it on your machine to see it in action.
📢 If you're running into any issues while going through the integration process, feel free to contact us at [email protected].