Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/llmo-referral-traffic/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ async function createWorkbook(results) {
return workbook;
}

async function enablePageIntentAudit(site, context, log) {
const { Configuration } = context.dataAccess;
const configuration = await Configuration.findLatest();

if (configuration.isHandlerEnabledForSite('page-intent', site)) {
log.debug(`[llmo-referral-traffic] page-intent is already enabled for site ${site.getId()}`);
return;
}

configuration.enableHandlerForSite('page-intent', site);
await configuration.save();
log.info(`[llmo-referral-traffic] Enabled page-intent audit for site ${site.getId()}`);
}

export async function triggerTrafficAnalysisImport(context) {
const {
site, finalUrl, log, auditContext = {},
Expand Down Expand Up @@ -153,6 +167,12 @@ export async function referralTrafficRunner(context) {
};
}

try {
await enablePageIntentAudit(site, context, log);
} catch (error) {
log.warn(`[llmo-referral-traffic] Failed to enable page-intent audit for site ${siteId}: ${error.message}`);
}

log.info('[llmo-referral-traffic] Enriching data with page intents and region information');
const pageIntents = await site.getPageIntents();
log.info(`[llmo-referral-traffic] Retrieved ${pageIntents.length} page intents for site ${siteId}`);
Expand Down
28 changes: 27 additions & 1 deletion test/audits/llmo-referral-traffic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('LLMO Referral Traffic Handler', () => {
let audit;
let mockAthenaClient;
let mockSharepointClient;
let mockConfiguration;
let getStaticContentStub;
let createLLMOSharepointClientStub;
let saveExcelReportStub;
Expand Down Expand Up @@ -68,6 +69,12 @@ describe('LLMO Referral Traffic Handler', () => {
}),
};

mockConfiguration = {
isHandlerEnabledForSite: sandbox.stub().returns(false),
enableHandlerForSite: sandbox.stub(),
save: sandbox.stub().resolves(),
};

context = new MockContextBuilder()
.withSandbox(sandbox)
.withOverrides({
Expand All @@ -78,6 +85,7 @@ describe('LLMO Referral Traffic Handler', () => {
},
})
.build();
context.dataAccess.Configuration.findLatest.resolves(mockConfiguration);

// Mock the handler module with esmock
handlerModule = await esmock('../../src/llmo-referral-traffic/handler.js', {
Expand Down Expand Up @@ -113,6 +121,9 @@ describe('LLMO Referral Traffic Handler', () => {
expect(result.fullAuditRef).to.include('No OpTel Data Found');
expect(mockAthenaClient.query).to.have.been.calledOnce;
expect(saveExcelReportStub).to.not.have.been.called;
expect(context.dataAccess.Configuration.findLatest).to.not.have.been.called;
expect(mockConfiguration.enableHandlerForSite).to.not.have.been.called;
expect(mockConfiguration.save).to.not.have.been.called;
});

it('should create populated spreadsheet when traffic data exists', async () => {
Expand Down Expand Up @@ -141,6 +152,9 @@ describe('LLMO Referral Traffic Handler', () => {
expect(result.fullAuditRef).to.equal('test-folder/referral-traffic/referral-traffic-w10-2025.xlsx');
expect(mockAthenaClient.query).to.have.been.calledOnce;
expect(saveExcelReportStub).to.have.been.calledOnce;
expect(context.dataAccess.Configuration.findLatest).to.have.been.calledOnce;
expect(mockConfiguration.enableHandlerForSite).to.have.been.calledWith('page-intent', site);
expect(mockConfiguration.save).to.have.been.calledOnce;
});

it('should enrich data with page intents and region', async () => {
Expand All @@ -161,6 +175,18 @@ describe('LLMO Referral Traffic Handler', () => {
expect(result.auditResult.rowCount).to.equal(2);
expect(saveExcelReportStub).to.have.been.calledOnce;
});

it('should not save configuration when page-intent is already enabled', async () => {
mockConfiguration.isHandlerEnabledForSite.withArgs('page-intent', site).returns(true);

mockAthenaClient.query.resolves([
{ path: '/page1', trf_type: 'earned', trf_channel: 'llm' },
]);

await handlerModule.referralTrafficRunner(context);

expect(mockConfiguration.enableHandlerForSite).to.not.have.been.called;
expect(mockConfiguration.save).to.not.have.been.called;
});
});
});