Skip to content

@get-advantage/advantage / advantage/messaging/creative-side / AdvantageCreativeMessenger

Class: AdvantageCreativeMessenger

advantage/messaging/creative-side.AdvantageCreativeMessenger

AdvantageCreativeMessenger is the class that should be used in creative ads to communicate with Advantage on the publisher side. This class is used to request formats and other information from the parent website.

Example

Here's an example on how to request a format from the parent website and then start the ad when the format is confirmed:

typescript
const advantageMessenger = new AdvantageCreativeMessenger();
const session = await advantageMessenger.startSession();

if (session) {
 // Request the midscroll format
 const response = await advantageMessenger.sendMessage({
     action: AdvantageMessageAction.REQUEST_FORMAT,
     format: AdvantageFormatName.Midscroll
 });
 // The format is confirmed, start the ad
 if (response?.action === AdvantageMessageAction.FORMAT_CONFIRMED) {
     document.body.classList.add("midscroll");
 }
} else {
     console.log("Session failed to start");
}
javascript
const advantageMessenger = new AdvantageCreativeMessenger();
advantageMessenger.startSession().then((confirmed) => {
 if (confirmed) {
     advantageMessenger.sendMessage({
         action: "REQUEST_FORMAT",
         format: "MIDSCROLL"
     }).then((response) => {
         if (response.action === "FORMAT_CONFIRMED") {
             // The format is confirmed, start the ad
             document.body.classList.add("midscroll");
         }
     });
 } else {
    console.log("Session failed to start");
 }
});

Constructors

constructor

new AdvantageCreativeMessenger(): AdvantageCreativeMessenger

Returns

AdvantageCreativeMessenger

Defined in

src/advantage/messaging/creative-side.ts:58

Properties

#messageChannel

Private #messageChannel: null | MessageChannel = null

Defined in

src/advantage/messaging/creative-side.ts:54


#sessionID

Private #sessionID: string

Defined in

src/advantage/messaging/creative-side.ts:55


#validSession

Private #validSession: boolean = false

Defined in

src/advantage/messaging/creative-side.ts:56

Methods

onMessage

onMessage(callback): void

Parameters

NameType
callback(message: AdvantageMessage) => void

Returns

void

Defined in

src/advantage/messaging/creative-side.ts:100


sendMessage

sendMessage(message): Promise<undefined | AdvantageMessage>

Parameters

NameType
messagePartial<AdvantageMessage>

Returns

Promise<undefined | AdvantageMessage>

Defined in

src/advantage/messaging/creative-side.ts:79


startSession

startSession(): Promise<boolean>

Returns

Promise<boolean>

Defined in

src/advantage/messaging/creative-side.ts:62