schema icon indicating copy to clipboard operation
schema copied to clipboard

Embed WebSite type with SearchAction automatically

Open brotkrueml opened this issue 6 years ago • 5 comments

As a SEO specialist I want to have the WebSite type with a SearchAction embedded on the homepage of a web site. Example:

{
    "@context": "https://schema.org",
    "@type": "WebSite",
    "url": "https://www.example.com/",
    "potentialAction": {
        "@type": "SearchAction",
        "target": "https://query.example.com/search?q={search_term_string}",
        "query-input": "required name=search_term_string"
    }
}

More information: https://developers.google.com/search/docs/data-types/sitelinks-searchbox

Also interesting: https://stackoverflow.com/questions/46770786/schema-org-for-website-with-multiple-languages-filling-corporation-and-website

  • [ ] The query-input parameter (e.g. q={search_term_string}) can be configured in the extension configuration.
  • [ ] The target page (search page) can be defined in the site configuration for each site and language.
  • [ ] If a target and a query-input parameter are configured, the markup for the search box is embedded on the homepage of the site/language.
  • [ ] The documentation is adjusted.
  • [ ] An entry to the changelog is available.

brotkrueml avatar Nov 08 '19 20:11 brotkrueml

@brotkrueml

What is the status of this, i am unable to use the schema extension since SearchAction does not contain query-input

support-beech avatar Dec 05 '22 15:12 support-beech

@support-beech I have it on my roadmap, it is scheduled for 2.7 (planned for January/February '23).

You can add the "query-input" property with a PSR-14 event: https://docs.typo3.org/p/brotkrueml/schema/2.6/en-us/Developer/Events.html#register-additional-properties-for-a-type

In the event listener:

if ($event->getType() === SearchAction::class) {
    $event->registerAdditionalProperty('query-input');
}

Then you can use it:

$searchAction->setProperty('query-action', 'required name=search_term_string'); 

Background: The types and properties in this extension are generated automatically from the JSON-LD schema. "query-input" is a custom property from Google, so not available in the official JSON-LD schema.

Main problem I face is that I found no documentation how to handle that in multi-language sites (which use sub-directories for languages). I assume, every language homepage has its own WebSite/SearchAction entry with the query-input? I checked multiple websites (from Apple, Microsoft, ...) but only found examples for the main language (it is some time ago when I did this). Do you have more insights?

brotkrueml avatar Dec 05 '22 16:12 brotkrueml

@brotkrueml

Thanks,

What i've done, is adding the searchAction through a dataprocessor.

        $searchResultsPageUri = $this->getUriBuilder()
            ->reset()
            ->setCreateAbsoluteUri(1)
            ->setTargetPageUid(self::SEARCH_PAGE_UID)
            ->build();

        $entryPoint = TypeFactory::createType('EntryPoint');
        $entryPoint->setProperty('urlTemplate', $searchResultsPageUri . '?q={search_term_string}');

        $searchAction = TypeFactory::createType('SearchAction');
        $searchAction->setProperty('target', $entryPoint);
        $searchAction->setProperty('query-input', 'required name=search_term_string');

        $webSite = TypeFactory::createType('WebSite');
        $webSite->setProperty('potentialAction', $searchAction);
        $webSite->setProperty('url', GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));

        $this->schemaManager->addType($webSite);

This will add the structured data on every page, i could also adjust it if i would like it on just the homepage. I am by no means an expert yet with structured data so we will have to check if this works. This will be done on a site with +/- 52 TYPO3 languages. The URL uri will always be for the current language site.

support-beech avatar Dec 06 '22 09:12 support-beech

@support-beech

Looks good so far, but: setCreateAbsoluteUri expects a bool, so set it to true (with TYPO3 v11 it is also enforced by PHP type hint).

According to google it must be added only to the home page: https://developers.google.com/search/docs/appearance/structured-data/sitelinks-searchbox?hl=en#adding-structured-data (point 2).

brotkrueml avatar Dec 06 '22 10:12 brotkrueml

@brotkrueml Thanks, will make sure this is only done on the homepage then and look into the bool.

support-beech avatar Dec 07 '22 08:12 support-beech