-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add content types in SR URL Inspector Filters response #2884
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright 2026 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 { resolveElementModel } from '../constants.js'; | ||
|
|
||
| // Legacy default window is a rolling 28 days (see defaultDateRange in | ||
| // llmo-brand-presence.js / cited-domains.js). Kept inline here so this definition | ||
| // stays pure and does not import controller code. | ||
| const DEFAULT_WINDOW_DAYS = 28; | ||
|
|
||
| function defaultDateRange() { | ||
| const end = new Date(); | ||
| const start = new Date(); | ||
| start.setDate(start.getDate() - DEFAULT_WINDOW_DAYS); | ||
| return { | ||
| startDate: start.toISOString().slice(0, 10), | ||
| endDate: end.toISOString().slice(0, 10), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the payload for the Content Types filter-dimensions element (d1f9e6ec). | ||
| * Returns the available `domain_type` values (Owned/Other/Social/Earned/Benchmark | ||
| * Competitors) for the given model + date window — powers the Content Type filter | ||
| * dropdown. | ||
| * | ||
| * @param {object} [params] | ||
| * @param {string} [params.model] - AI model (Semrush engine name or SpaceCat/UI platform code). | ||
| * Translated to a Semrush model + validated via {@link resolveElementModel} (default search-gpt). | ||
| * @param {string} [params.platform] - Legacy alias for `model`; `model` takes precedence. | ||
| * @param {string} [params.startDate] - ISO date (YYYY-MM-DD). Defaults to 28 days ago. | ||
| * @param {string} [params.endDate] - ISO date (YYYY-MM-DD). Defaults to today. | ||
| */ | ||
| export function buildContentTypesPayload({ | ||
| model, platform, startDate, endDate, | ||
| } = {}) { | ||
| const resolvedModel = resolveElementModel(model || platform); | ||
| const defaults = defaultDateRange(); | ||
| const start = startDate || defaults.startDate; | ||
| const end = endDate || defaults.endDate; | ||
|
|
||
| return { | ||
| comparison_data_formatting: 'union', | ||
| filters: { | ||
| simple: {}, | ||
| advanced: { | ||
| op: 'and', | ||
| filters: [ | ||
| { op: 'eq', val: resolvedModel, col: 'CBF_model' }, | ||
| { op: 'gte', val: start, col: 'CBF_date__start' }, | ||
| { op: 'lte', val: end, col: 'CBF_date__end' }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Transforms the raw Semrush Content Types element response into URL Inspector | ||
| * filter-dimension content types. `id` is derived from `label` (lowercased, | ||
| * spaces → underscores) — Semrush has no stable content-type ID. | ||
| * | ||
| * @param {object} raw - Raw response from the Elements API. | ||
| * @returns {Array<{id: string, label: string}>} | ||
| */ | ||
| export function transformContentTypesToFilterDimensions(raw) { | ||
| return (raw?.blocks?.value ?? []) | ||
| .map((item) => String(item.value ?? '')) | ||
| .filter((label) => label !== '') | ||
| .map((label) => ({ | ||
| id: label.toLowerCase().replace(/\s+/g, '_'), | ||
| label, | ||
| })); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
test/support/elements/definitions/content-types.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * Copyright 2026 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 { expect } from 'chai'; | ||
| import { | ||
| buildContentTypesPayload, | ||
| transformContentTypesToFilterDimensions, | ||
| } from '../../../../src/support/elements/definitions/content-types.js'; | ||
| import { DEFAULT_ELEMENT_MODEL } from '../../../../src/support/elements/constants.js'; | ||
|
|
||
| describe('content-types definitions', () => { | ||
| describe('buildContentTypesPayload', () => { | ||
| it('uses the default model when no params are provided', () => { | ||
| const payload = buildContentTypesPayload(); | ||
| expect(payload.filters.advanced.filters[0]).to.deep.equal({ | ||
| op: 'eq', | ||
| val: DEFAULT_ELEMENT_MODEL, | ||
| col: 'CBF_model', | ||
| }); | ||
| }); | ||
|
|
||
| it('uses the default model when model is not in ELEMENT_MODELS', () => { | ||
| const payload = buildContentTypesPayload({ model: 'unknown-model' }); | ||
| expect(payload.filters.advanced.filters[0].val).to.equal(DEFAULT_ELEMENT_MODEL); | ||
| }); | ||
|
|
||
| it('uses the provided model when it is in ELEMENT_MODELS', () => { | ||
| const payload = buildContentTypesPayload({ model: 'perplexity' }); | ||
| expect(payload.filters.advanced.filters[0].val).to.equal('perplexity'); | ||
| }); | ||
|
|
||
| it('translates a SpaceCat/UI platform code to the Semrush model', () => { | ||
| const payload = buildContentTypesPayload({ model: 'copilot' }); | ||
| expect(payload.filters.advanced.filters[0].val).to.equal('microsoft-copilot'); | ||
| }); | ||
|
|
||
| it('accepts the platform alias and translates it', () => { | ||
| const payload = buildContentTypesPayload({ platform: 'openai' }); | ||
| expect(payload.filters.advanced.filters[0].val).to.equal('chatgpt-paid'); | ||
| }); | ||
|
|
||
| it('uses the provided startDate and endDate', () => { | ||
| const payload = buildContentTypesPayload({ startDate: '2026-06-07', endDate: '2026-06-14' }); | ||
| expect(payload.filters.advanced.filters[1]).to.deep.equal({ | ||
| op: 'gte', | ||
| val: '2026-06-07', | ||
| col: 'CBF_date__start', | ||
| }); | ||
| expect(payload.filters.advanced.filters[2]).to.deep.equal({ | ||
| op: 'lte', | ||
| val: '2026-06-14', | ||
| col: 'CBF_date__end', | ||
| }); | ||
| }); | ||
|
|
||
| it('defaults to a rolling 28-day window when no dates are provided', () => { | ||
| const payload = buildContentTypesPayload(); | ||
| const start = payload.filters.advanced.filters[1]; | ||
| const end = payload.filters.advanced.filters[2]; | ||
| expect(start.col).to.equal('CBF_date__start'); | ||
| expect(end.col).to.equal('CBF_date__end'); | ||
| const startMs = new Date(start.val).getTime(); | ||
| const endMs = new Date(end.val).getTime(); | ||
| const daysApart = Math.round((endMs - startMs) / (24 * 60 * 60 * 1000)); | ||
| expect(daysApart).to.equal(28); | ||
| }); | ||
|
|
||
| it('sets comparison_data_formatting to union', () => { | ||
| const payload = buildContentTypesPayload(); | ||
| expect(payload.comparison_data_formatting).to.equal('union'); | ||
| }); | ||
|
|
||
| it('uses AND operator in the advanced filter', () => { | ||
| const payload = buildContentTypesPayload(); | ||
| expect(payload.filters.advanced.op).to.equal('and'); | ||
| }); | ||
|
|
||
| it('includes an empty simple filter object', () => { | ||
| const payload = buildContentTypesPayload(); | ||
| expect(payload.filters.simple).to.deep.equal({}); | ||
| }); | ||
| }); | ||
|
|
||
| describe('transformContentTypesToFilterDimensions', () => { | ||
| it('returns an empty array when raw is null', () => { | ||
| expect(transformContentTypesToFilterDimensions(null)).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('returns an empty array when raw has no blocks', () => { | ||
| expect(transformContentTypesToFilterDimensions({})).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('derives id from a single-word label', () => { | ||
| const raw = { blocks: { value: [{ value: 'Owned' }] } }; | ||
| const [item] = transformContentTypesToFilterDimensions(raw); | ||
| expect(item).to.deep.equal({ id: 'owned', label: 'Owned' }); | ||
| }); | ||
|
|
||
| it('derives id from a multi-word label by replacing spaces with underscores', () => { | ||
| const raw = { blocks: { value: [{ value: 'Benchmark Competitors' }] } }; | ||
| const [item] = transformContentTypesToFilterDimensions(raw); | ||
| expect(item).to.deep.equal({ id: 'benchmark_competitors', label: 'Benchmark Competitors' }); | ||
| }); | ||
|
|
||
| it('filters out entries with an empty/missing value', () => { | ||
| const raw = { blocks: { value: [{ value: '' }, { }, { value: 'Social' }] } }; | ||
| const result = transformContentTypesToFilterDimensions(raw); | ||
| expect(result).to.deep.equal([{ id: 'social', label: 'Social' }]); | ||
| }); | ||
|
|
||
| it('handles the full known set of content types, preserving element order', () => { | ||
| const raw = { | ||
| blocks: { | ||
| value: [ | ||
| { value: 'Other' }, | ||
| { value: 'Social' }, | ||
| { value: 'Earned' }, | ||
| { value: 'Owned' }, | ||
| { value: 'Benchmark Competitors' }, | ||
| ], | ||
| }, | ||
| }; | ||
| const result = transformContentTypesToFilterDimensions(raw); | ||
| expect(result).to.deep.equal([ | ||
| { id: 'other', label: 'Other' }, | ||
| { id: 'social', label: 'Social' }, | ||
| { id: 'earned', label: 'Earned' }, | ||
| { id: 'owned', label: 'Owned' }, | ||
| { id: 'benchmark_competitors', label: 'Benchmark Competitors' }, | ||
| ]); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.