Skip to content

feat: Low form views auto detect #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/forms-opportunities/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
export const FORM_OPPORTUNITY_TYPES = {
LOW_CONVERSION: 'high-form-views-low-conversions',
LOW_NAVIGATION: 'high-page-views-low-form-nav',
LOW_VIEWS: 'high-page-views-low-form-views',
};
8 changes: 3 additions & 5 deletions src/forms-opportunities/formcalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function aggregateFormVitalsByDevice(formVitalsCollection) {
formVitalsCollection.forEach((item) => {
const {
url, formview = {}, formengagement = {}, pageview = {}, formsubmit = {},
trafficacquisition = {},
trafficacquisition = {}, formsource = '',
} = item;

const totals = {
Expand Down Expand Up @@ -56,6 +56,7 @@ function aggregateFormVitalsByDevice(formVitalsCollection) {
totals.pageview = calculateSums(pageview, totals.pageview);
totals.formsubmit = calculateSums(formsubmit, totals.formsubmit);
totals.trafficacquisition = trafficacquisition;
totals.formsource = formsource;
resultMap.set(url, totals);
});

Expand Down Expand Up @@ -116,14 +117,11 @@ export function getHighPageViewsLowFormViewsMetrics(formVitalsCollection) {
resultMap.forEach((metrics, url) => {
const { total: pageViews } = metrics.pageview;
const { total: formViews } = metrics.formview;
const { total: formEngagement } = metrics.formengagement;

if (hasHighPageViews(pageViews) && hasLowFormViews(pageViews, formViews)) {
urls.push({
url,
pageViews,
formViews,
formEngagement,
...metrics,
});
}
});
Expand Down
7 changes: 5 additions & 2 deletions src/forms-opportunities/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { generateOpptyData } from './utils.js';
import { getScrapedDataForSiteId } from '../support/utils.js';
import createLowConversionOpportunities from './oppty-handlers/low-conversion-handler.js';
import createLowNavigationOpportunities from './oppty-handlers/low-navigation-handler.js';
import createLowViewsOpportunities from './oppty-handlers/low-views-handler.js';

const { AUDIT_STEP_DESTINATIONS } = Audit;
const FORMS_OPPTY_QUERIES = [
Expand Down Expand Up @@ -100,8 +101,10 @@ export async function processOpportunityStep(context) {
log.info(`[Form Opportunity] [Site Id: ${site.getId()}] processing opportunity`);
const scrapedData = await getScrapedDataForSiteId(site, context);
const latestAudit = await site.getLatestAuditByAuditType('forms-opportunities');
await createLowConversionOpportunities(finalUrl, latestAudit, scrapedData, context);
await createLowNavigationOpportunities(finalUrl, latestAudit, scrapedData, context);
const excludeForms = new Set();
await createLowNavigationOpportunities(finalUrl, latestAudit, scrapedData, context, excludeForms);
await createLowViewsOpportunities(finalUrl, latestAudit, scrapedData, context, excludeForms);
await createLowConversionOpportunities(finalUrl, latestAudit, scrapedData, context, excludeForms);
log.info(`[Form Opportunity] [Site Id: ${site.getId()}] opportunity identified`);
return {
status: 'complete',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ function generateDefaultGuidance(scrapedData, oppoty) {
* @param auditUrl - The URL of the audit
* @param auditData - The audit data containing the audit result and additional details.
* @param context - The context object containing the data access and logger objects.
* @param excludeForms - A set of Forms to exclude from the opportunity creation process.
*/
// eslint-disable-next-line max-len
export default async function createLowConversionOpportunities(auditUrl, auditDataObject, scrapedData, context) {
export default async function createLowConversionOpportunities(auditUrl, auditDataObject, scrapedData, context, excludeForms = new Set()) {
const {
dataAccess, log, sqs, site, env,
} = context;
Expand All @@ -107,9 +108,9 @@ export default async function createLowConversionOpportunities(auditUrl, auditDa
// eslint-disable-next-line max-len
const formOpportunities = await generateOpptyData(formVitals, context, [FORM_OPPORTUNITY_TYPES.LOW_CONVERSION]);
log.debug(`forms opportunities ${JSON.stringify(formOpportunities, null, 2)}`);
const filteredOpportunities = filterForms(formOpportunities, scrapedData, log);
const filteredOpportunities = filterForms(formOpportunities, scrapedData, log, excludeForms);
filteredOpportunities.forEach((oppty) => excludeForms.add(oppty.form + oppty.formsource));
log.info(`filtered opportunties high form views low conversion for form ${JSON.stringify(filteredOpportunities, null, 2)}`);

try {
for (const opptyData of filteredOpportunities) {
let highFormViewsLowConversionsOppty = opportunities.find(
Expand Down
14 changes: 10 additions & 4 deletions src/forms-opportunities/oppty-handlers/low-navigation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const formPathSegments = ['contact', 'newsletter', 'sign', 'enrol', 'subscribe',
* @param auditUrl - The URL of the audit
* @param auditData - The audit data containing the audit result and additional details.
* @param context - The context object containing the data access and logger objects.
* @param excludeForms - A set of Forms to exclude from the opportunity creation process.
*/
// eslint-disable-next-line max-len
export default async function createLowNavigationOpportunities(auditUrl, auditDataObject, scrapedData, context) {
export default async function createLowNavigationOpportunities(auditUrl, auditDataObject, scrapedData, context, excludeForms = new Set()) {
const { dataAccess, log } = context;
const { Opportunity } = dataAccess;

Expand All @@ -47,14 +48,19 @@ export default async function createLowNavigationOpportunities(auditUrl, auditDa
const filteredOpportunitiesByNavigation = formOpportunities.filter((opportunity) => formPathSegments.some((substring) => opportunity.form?.includes(substring)
&& !opportunity.formNavigation?.url?.includes('search')));

const filteredOpportunities = filterForms(filteredOpportunitiesByNavigation, scrapedData, log);
const filteredOpportunities = filterForms(
filteredOpportunitiesByNavigation,
scrapedData,
log,
excludeForms,
);
filteredOpportunities.forEach((oppty) => excludeForms.add(oppty.form + oppty.formsource));
log.info(`filtered opportunities: high-page-views-low-form-navigations: ${JSON.stringify(filteredOpportunities, null, 2)}`);

try {
for (const opptyData of filteredOpportunities) {
let highPageViewsLowFormNavOppty = opportunities.find(
(oppty) => oppty.getType() === FORM_OPPORTUNITY_TYPES.LOW_NAVIGATION
&& oppty.getData().form === opptyData.form,
&& oppty.getData().form === opptyData.form,
);

const opportunityData = {
Expand Down
96 changes: 96 additions & 0 deletions src/forms-opportunities/oppty-handlers/low-views-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import { FORM_OPPORTUNITY_TYPES } from '../constants.js';
import { filterForms, generateOpptyData } from '../utils.js';

/**
* @param auditUrl - The URL of the audit
* @param auditData - The audit data containing the audit result and additional details.
* @param context - The context object containing the data access and logger objects.
* @param excludeForms - A set of Forms to exclude from the opportunity creation process.
*/
// eslint-disable-next-line max-len
export default async function createLowViewsOpportunities(auditUrl, auditDataObject, scrapedData, context, excludeForms = new Set()) {
const { dataAccess, log } = context;
const { Opportunity } = dataAccess;

const auditData = JSON.parse(JSON.stringify(auditDataObject));
log.info(`Syncing high page views low form views opportunity for ${auditData.siteId}`);
let opportunities;

try {
opportunities = await Opportunity.allBySiteIdAndStatus(auditData.siteId, 'NEW');
} catch (e) {
log.error(`Fetching opportunities for siteId ${auditData.siteId} failed with error: ${e.message}`);
throw new Error(`Failed to fetch opportunities for siteId ${auditData.siteId}: ${e.message}`);
}

const { formVitals } = auditData.auditResult;
// eslint-disable-next-line max-len
const formOpportunities = await generateOpptyData(formVitals, context, [FORM_OPPORTUNITY_TYPES.LOW_VIEWS]);
log.debug(`forms opportunities high-page-views-low-form-views: ${JSON.stringify(formOpportunities, null, 2)}`);

const filteredOpportunities = filterForms(formOpportunities, scrapedData, log, excludeForms);
filteredOpportunities.forEach((oppty) => excludeForms.add(oppty.form + oppty.formsource));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we update this excludeForms set in filterForms method itself?

log.info(`filtered opportunities: high-page-views-low-form-views: ${JSON.stringify(filteredOpportunities, null, 2)}`);
try {
for (const opptyData of filteredOpportunities) {
let highPageViewsLowFormViewsOptty = opportunities.find(
(oppty) => oppty.getType() === FORM_OPPORTUNITY_TYPES.LOW_VIEWS
&& oppty.getData().form === opptyData.form,
);

const opportunityData = {
siteId: auditData.siteId,
auditId: auditData.auditId,
runbook: 'https://adobe.sharepoint.com/:w:/s/AEM_Forms/EeYKNa4HQkRAleWXjC5YZbMBMhveB08F1yTTUQSrP97Eow?e=cZdsnA',
type: FORM_OPPORTUNITY_TYPES.LOW_VIEWS,
origin: 'AUTOMATION',
title: 'The form has low views',
description: 'The form has low views but the page containing the form has higher traffic',
tags: ['Forms Conversion'],
data: {
...opptyData,
},
guidance: {
recommendations: [
{
insight: `The form in the page: ${opptyData.form} has low discoverability and only ${(opptyData.formViews / opptyData.pageViews) * 100}% visitors landing on the page are viewing the form.`,
recommendation: 'Position the form higher up on the page so users see it without scrolling. Consider using clear and compelling CTAs, minimizing distractions, and ensuring strong visibility across devices.',
type: 'guidance',
rationale: 'Forms that are visible above the fold are more likely to be seen and interacted with by users.',
},
],
},
};

log.info(`Forms Opportunity created high page views low form views ${JSON.stringify(opportunityData, null, 2)}`);
if (!highPageViewsLowFormViewsOptty) {
// eslint-disable-next-line no-await-in-loop
highPageViewsLowFormViewsOptty = await Opportunity.create(opportunityData);
} else {
highPageViewsLowFormViewsOptty.setAuditId(auditData.auditId);
highPageViewsLowFormViewsOptty.setData({
...highPageViewsLowFormViewsOptty.getData(),
...opportunityData.data,
});
highPageViewsLowFormViewsOptty.setGuidance(opportunityData.guidance);
// eslint-disable-next-line no-await-in-loop
await highPageViewsLowFormViewsOptty.save();
}
}
} catch (e) {
log.error(`Creating Forms opportunity for high page views low form views for siteId ${auditData.siteId} failed with error: ${e.message}`, e);
}
log.info(`Successfully synced Opportunity for site: ${auditData.siteId} and high page views low form views audit type.`);
}
28 changes: 22 additions & 6 deletions src/forms-opportunities/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import {
getHighPageViewsLowFormCtrMetrics, getHighFormViewsLowConversionMetrics,
getHighPageViewsLowFormViewsMetrics,
} from './formcalc.js';
import { FORM_OPPORTUNITY_TYPES } from './constants.js';

Expand Down Expand Up @@ -90,10 +91,10 @@ function getFormMetrics(metricObject) {
});
}

function convertToLowNavOpptyData(metricObject) {
function convertToLowViewOpptyData(metricObject) {
const {
formview: { total: formViews, mobile: formViewsMobile, desktop: formViewsDesktop },
CTA, trafficacquisition,
trafficacquisition,
} = metricObject;
return {
trackedFormKPIName: 'Form Views',
Expand Down Expand Up @@ -131,10 +132,18 @@ function convertToLowNavOpptyData(metricObject) {
},
},
],
formNavigation: CTA,
};
}

function convertToLowNavOpptyData(metricObject) {
const {
CTA,
} = metricObject;
const opptyData = convertToLowViewOpptyData(metricObject);
opptyData.formNavigation = CTA;
return opptyData;
}

function convertToLowConversionOpptyData(metricObject) {
const { trafficacquisition } = metricObject;
const deviceWiseMetrics = getFormMetrics(metricObject);
Expand Down Expand Up @@ -187,6 +196,7 @@ function convertToLowConversionOpptyData(metricObject) {
async function convertToOpportunityData(opportunityType, metricObject, context) {
const {
url, pageview: { total: pageViews }, formview: { total: formViews },
formsource = '',
} = metricObject;

const {
Expand All @@ -204,12 +214,15 @@ async function convertToOpportunityData(opportunityType, metricObject, context)
opportunityData = convertToLowConversionOpptyData(metricObject);
} else if (opportunityType === FORM_OPPORTUNITY_TYPES.LOW_NAVIGATION) {
opportunityData = convertToLowNavOpptyData(metricObject);
} else if (opportunityType === FORM_OPPORTUNITY_TYPES.LOW_VIEWS) {
opportunityData = convertToLowViewOpptyData(metricObject);
}

const screenshot = await getPresignedUrl('screenshot-desktop-fullpage.png', context, url, site);
opportunityData = {
...opportunityData,
form: url,
formsource,
formViews,
pageViews,
screenshot,
Expand All @@ -222,7 +235,8 @@ async function convertToOpportunityData(opportunityType, metricObject, context)
export async function generateOpptyData(
formVitals,
context,
opportunityTypes = [FORM_OPPORTUNITY_TYPES.LOW_CONVERSION, FORM_OPPORTUNITY_TYPES.LOW_NAVIGATION],
opportunityTypes = [FORM_OPPORTUNITY_TYPES.LOW_CONVERSION,
FORM_OPPORTUNITY_TYPES.LOW_NAVIGATION, FORM_OPPORTUNITY_TYPES.LOW_VIEWS],
) {
const formVitalsCollection = formVitals.filter(
(row) => row.formengagement && row.formsubmit && row.formview,
Expand All @@ -231,6 +245,7 @@ export async function generateOpptyData(
Object.entries({
[FORM_OPPORTUNITY_TYPES.LOW_CONVERSION]: getHighFormViewsLowConversionMetrics,
[FORM_OPPORTUNITY_TYPES.LOW_NAVIGATION]: getHighPageViewsLowFormCtrMetrics,
[FORM_OPPORTUNITY_TYPES.LOW_VIEWS]: getHighPageViewsLowFormViewsMetrics,
})
.filter(([opportunityType]) => opportunityTypes.includes(opportunityType))
.flatMap(([opportunityType, metricsMethod]) => metricsMethod(formVitalsCollection)
Expand All @@ -249,12 +264,13 @@ export function shouldExcludeForm(scrapedFormData) {
* @param formOpportunities
* @param scrapedData
* @param log
* @param excludeUrls urls to exclude from opportunity creation
* @returns {*}
*/
export function filterForms(formOpportunities, scrapedData, log) {
export function filterForms(formOpportunities, scrapedData, log, excludeUrls = new Set()) {
return formOpportunities.filter((opportunity) => {
let urlMatches = false;
if (opportunity.form.includes('search')) {
if (opportunity.form.includes('search') || excludeUrls.has(opportunity.form + opportunity.formsource)) {
return false; // exclude search pages
}
if (isNonEmptyArray(scrapedData?.formData)) {
Expand Down
21 changes: 15 additions & 6 deletions test/audits/forms/formcalc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Form Calc functions', () => {
pageview: { total: 8670, desktop: 4670, mobile: 4000 },
url: 'https://www.surest.com/info/win',
trafficacquisition: {},
formsource: '.myform',
},
{
formengagement: { total: 300, desktop: 0, mobile: 300 },
Expand All @@ -39,6 +40,7 @@ describe('Form Calc functions', () => {
pageview: { total: 8670, desktop: 4670, mobile: 4000 },
url: 'https://www.surest.com/newsletter',
trafficacquisition: {},
formsource: '',
},
]);
});
Expand All @@ -48,15 +50,21 @@ describe('Form Calc functions', () => {
expect(result).to.eql([
{
url: 'https://www.surest.com/info/win',
pageViews: 8670,
formViews: 300,
formEngagement: 4300,
formengagement: { total: 4300, desktop: 4000, mobile: 300 },
formsubmit: { total: 0, desktop: 0, mobile: 0 },
formview: { total: 300, desktop: 0, mobile: 300 },
pageview: { total: 8670, desktop: 4670, mobile: 4000 },
trafficacquisition: {},
formsource: '.myform',
},
{
url: 'https://www.surest.com/newsletter',
pageViews: 8670,
formViews: 300,
formEngagement: 300,
formengagement: { total: 300, desktop: 0, mobile: 300 },
formsubmit: { total: 0, desktop: 0, mobile: 0 },
formview: { total: 300, desktop: 0, mobile: 300 },
pageview: { total: 8670, desktop: 4670, mobile: 4000 },
trafficacquisition: {},
formsource: '',
},
]);
});
Expand All @@ -70,6 +78,7 @@ describe('Form Calc functions', () => {
formview: { total: 300, desktop: 0, mobile: 300 },
formengagement: { total: 300, desktop: 0, mobile: 300 },
formsubmit: { total: 0, desktop: 0, mobile: 0 },
formsource: '',
trafficacquisition: {},
CTA: {
url: 'https://www.surest.com/about-us',
Expand Down
11 changes: 11 additions & 0 deletions test/audits/forms/low-conv-oppoty-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,15 @@ describe('createLowConversionOpportunities handler method', () => {
expect(dataAccessStub.Opportunity.create).to.be.calledWith(testData.opportunityData5);
expect(logStub.info).to.be.calledWith('Successfully synced Opportunity for site: site-id and high-form-views-low-conversions audit type.');
});

it('should not create low conversion opportunity if another opportunity already exists', async () => {
const excludeUrls = new Set();
excludeUrls.add('https://www.surest.com/newsletter');
excludeUrls.add('https://www.surest.com/info/win-1.form');
await createLowConversionOpportunities(auditUrl, auditData, undefined, context, excludeUrls);
expect(dataAccessStub.Opportunity.create).to.be.callCount(3);
expect(excludeUrls.has('https://www.surest.com/contact-us.mycontact')).to.be.true;
expect(excludeUrls.has('https://www.surest.com/info/win-2')).to.be.true;
expect(excludeUrls.has('https://www.surest.com/info/win')).to.be.true;
});
});
Loading