Moments
Mobile

React Native

22min

Introduction

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 Starting

Before diving into the integration process, there are a few preliminary steps you need to take

Obtain an API key

To access the Moments API, you must obtain an API key. Follow the instructions provided to acquire your unique API key.

Plan Your Implementation

  1. Determine where MomentScience Offers will display within your app.
  2. Decide whether Offers will appear as part of a screen or as full-screen modal overlays.
  3. Specify whether Offers should display once per session or each time a specific section of the app is accessed.

Getting Started

Let's kickstart the integration process by completing the following tasks:

Install Dependency Packages

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.

Dependency Packages Installation Command


Implement Intent Filter for Android App

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.

XML


Get Your Device User-Agent Parameter

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.

Fetching 'User-Agent' Parameter


Generating User Identifiers

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.

Generating Unique User ID


Fetch Offer Data

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.

Call Moments API


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.

Supporting Functions

In the previous code snippet, several functions are invoked to facilitate various operations. Here's an overview of each function and its purpose:

  1. firePixelFunction: This function is utilized to send requests to different URLs based on specific conditions:
    1. When a new offer is displayed on the screen.
    2. When a negative Call-To-Action (CTA) like "No thanks" is tapped.
    3. When the user taps on the Close button.
  2. 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.
firePixel Function
openURL Function


Implementing UI and Event Beacons

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.

UI Design

We'll split the UI into two main parts.

Part 1: Offer Container UI (OfferContainerView):

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.

JS




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:

JS


OfferContainerView Overview

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.

  • When a new offer is rendered, a request is sent to the pixel URL which we can get from .pixel
  • When the user taps on the close button, a request is sent to the `close beacon` URL which we can get it from .beacons?.close
  • When the user taps on a negative CTA, a request is sent to the beacons?.no_thanks_click URL.

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

JS


Part 2: Offer UI (OfferView):

OfferView Component Overview

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.

  • We need to fetch the click URL and then open it in an external browser, which is handled by the openURL function.

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:

  • We open the URL stored in .click_url in an external browser.
  • We move the user to the next offer by invoking goToNextOffer.

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:

  • We send a request using the URL stored in .beacons?.no_thanks_click.
  • We move the user to the next offer by calling goToNextOffer.



Complete Source Code of OfferView.js

OfferView.js Source Code


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].



Updated 21 Sep 2024
Did this page help you?