Brand Metrics (BM) - JavaScript API Guide

This guide is designed for BM Publisher customers to create audiences based on BM survey response data, suitable for integration with multiple DMPs and/or other systems

Implementing the BM JavaScript API

To collect and pass survey data to your a system of your choice, you can leverage BM’s JavaScript API. The following sample script outlines how to capture survey responses and format them for DMP integration.

Available events

You can listen to and attach logic to the following events happening within the BM ecosystem. The following list contains available events and the contents of the event object that is passed to the handler function.

survey_loaded Emits when a survey is loaded but not neccessarily shown.

{
    available: boolean;
    showed: boolean;
    mid?: string; // Measurement- id, set when available is true
}

survey_rendered Emits when a survey is rendered on the page.

{
    mid: string; // The measurement- id
}

survey_answer Emits when a user answers a survey question. For surveys with multiple questions, this event is emitted for each question answered.

{
    mid: string; // The measurement- id
    answers: string; // The answer(s) given
}

The answers is a semi- colon separated list of questions and answers following the structure of:
q{question index}-{option index}.
All indexes are 1- based, the first question is q1, the first option is 1.

example: q1-1;q2-1

If a question is a multiple choice question, the answer will be a comma separated list of selected options, e.g.

example: q1-1,3

survey_complete Emits when all questions in a survey has been answered.

{
    mid: string; // The measurement- id
}

survey_closed Emits when a survey is closed

{
    mid: string; // The measurement- id
}

JavaScript Code for Survey Event Handling

// Initialize the BM command array if it isn’t already created
window._brandmetrics = window._brandmetrics || [];

// Add the event handler to capture survey response data
window._brandmetrics.push({
    cmd: '_addeventlistener', val: {
        event: 'surveyanswered',
        handler: function(ev) {
            try {
                // Parse the most recent survey answer
                let questionAnswers = ev.answers.split(';').slice(-1)[0].split('-');
                let question = questionAnswers[0] || null;
                let answers = questionAnswers[1] ? questionAnswers[1].split(',') : null;

                // Replace the following line with your DMP’s tracking method
                // E.g., DMP.track('SurveyResponse', { mid: ev.mid, question: question, answers: answers });
            } catch (e) {
                console.error('BrandMetrics: Error handling survey response data', e);
            }
        },
    },
});