brotkrueml / schema

TYPO3 extension providing an API and view helpers for schema.org markup
https://extensions.typo3.org/extension/schema
GNU General Public License v2.0
32 stars 9 forks source link

Embed WebSite type with SearchAction automatically #24

Closed brotkrueml closed 2 weeks ago

brotkrueml commented 5 years ago

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

support-beech commented 1 year ago

@brotkrueml

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

brotkrueml commented 1 year ago

@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?

support-beech commented 1 year ago

@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.

brotkrueml commented 1 year ago

@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).

support-beech commented 1 year ago

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

brotkrueml commented 2 weeks ago

Google removed the Sitelinks Search Box, so no need to implement this now: https://developers.google.com/search/blog/2024/10/sitelinks-search-box