Skip to main content

⚙️ SDK Methods Reference

This document provides detailed information about all available methods in the Magfi Ad Network SDK.

Core Methods

getInstance(appCode)

Initializes and returns an instance of the Magfi Ad Network SDK.

Parameters

NameTypeDescription
APP_CODEstringYour unique application code provided by Magfi

Returns

TypeDescription
ObjectThe Magfi SDK instance

Example

const magfi = window.MagfiAdNetwork.getInstance("YOUR_APP_CODE");

add(options)

Creates and initializes an ad instance.

Parameters

NameTypeDescription
optionsObjectConfiguration options
options.codestringThe unique ad placement code

Returns

TypeDescription
AdInstanceAn instance of the ad

Example

// Basic usage
const ad = magfi.add({ AD_CODE: "AD_CODE" });

Ad Instance Methods

These methods are available on the ad instance returned by the add() method.

show(callbacks)

Displays the ad.

Parameters

NameTypeDescription
callbacksObject(Optional) Callback functions
callbacks.onClosedFunctionCalled when the ad is closed
callbacks.onGrantedFunctionCalled when permissions are granted
callbacks.onSkipFunctionCalled when ad is skipped

Returns

TypeDescription
voidNo return value

Example

ad.show({
onClosed: () => {
console.log("Ad was closed");
// Reward the user or continue app flow
},
onGranted: () => {
console.log("Permission granted");
},
onSkip: ()=>{
console.log("Ad was skipped");
}
});

isReady()

Checks if the ad is ready to be displayed.

Returns

TypeDescription
booleantrue if the ad is ready to be shown, otherwise false

Example

if (ad.isReady()) {
ad.show();
} else {
console.log("Ad not ready yet");
}

addEventListener(eventName, callback)

Adds an event listener to the ad instance.

Parameters

NameTypeDescription
eventNamestringThe name of the event to listen for
callbackFunctionThe function to execute when the event occurs

Returns

TypeDescription
voidNo return value

Example

ad.addEventListener("onReady", () => {
console.log("Ad is ready to be shown");
showAdButton.disabled = false;
});

ad.addEventListener("onError", () => {
console.log("Ad failed to load");
});

removeEventListener(eventName, callback)

Removes an event listener from the ad instance.

Parameters

NameTypeDescription
eventNamestringThe name of the event
callbackFunctionThe function that was originally passed to addEventListener

Returns

TypeDescription
voidNo return value

Example

const onReadyHandler = () => {
console.log("Ad is ready");
};

// Add the event listener
ad.addEventListener("onReady", onReadyHandler);

// Later, remove the event listener
ad.removeEventListener("onReady", onReadyHandler);

Events

These events can be used with the addEventListener method.

Event NameDescription
onReadyTriggered when the ad is loaded and ready to be displayed
onGrantedTriggered when the user grants the necessary permissions
onStartTriggered when the ad starts playing
onCloseTriggered when the ad is closed
onErrorTriggered when an error occurs during ad loading or display
adNotFoundTriggered when no ad is available for the requested placement
onSkipTriggered when the user skips the rewarded ad without receiving the reward

TypeScript Definitions

If you're using TypeScript, here are the type definitions for the SDK:

interface AdCallbacks {
onClosed?: () => void;
onGranted?: () => void;
onSkip?: () => void;
}

type AdEventName = "onReady" | "onGranted" | "onStart" | "onClose" | "onError" | "adNotFound" | "onSkip";

interface AdInstance {
show: (callbacks?: AdCallbacks) => void;
isReady: () => boolean;
addEventListener: (eventName: AdEventName, callback: () => void) => void;
removeEventListener: (eventName: AdEventName, callback: () => void) => void;
}

interface AdOptions {
AD_CODE string;
}

interface MagfiAdNetwork {
getInstance: (appCode: string) => {
add: (options: AdOptions) => AdInstance;
};
}

declare global {
interface Window {
MagfiAdNetwork: MagfiAdNetwork;
}
}

Error Handling

It's important to properly handle errors when working with ads. Here are some common error scenarios and how to handle them:

Ad Not Ready

Always check if the ad is ready before showing it:

if (ad.isReady()) {
ad.show();
} else {
console.log("Ad not ready yet");
// Show a loading indicator or alternative content
}

Ad Load Error

Listen for the onError event to detect when an ad fails to load:

ad.addEventListener("onError", () => {
console.log("Ad failed to load");
// Update UI to reflect error state
// Hide ad button or show alternative content
});

No Ad Available

Listen for the adNotFound event to detect when no ad is available:

ad.addEventListener("adNotFound", () => {
console.log("No ad available");
// Hide ad-related UI elements
// Show alternative content
});