Skip to content

Commit

Permalink
Add additional metadata fields
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 committed Jul 15, 2024
1 parent 2c6633f commit 609421e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
3 changes: 3 additions & 0 deletions tools/pdp-metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"main": "pdp-metadata.js",
"author": "",
"license": "Apache-2.0",
"scripts": {
"start": "node pdp-metadata.js"
},
"dependencies": {
"he": "^1.2.0",
"xlsx": "^0.18.5"
Expand Down
47 changes: 35 additions & 12 deletions tools/pdp-metadata/pdp-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import fs from 'fs';
import he from 'he';
import productSearchQuery from './queries/products.graphql.js';

const configFile = 'https://main--aem-boilerplate-commerce--hlxsites.hlx.live/configs.json?sheet=prod';
const basePath = 'https://main--aem-boilerplate-commerce--hlxsites.hlx.live';
const configFile = `${basePath}/configs.json?sheet=prod`;


async function performCatalogServiceQuery(config, query, variables) {
const headers = {
Expand All @@ -16,7 +18,7 @@ async function performCatalogServiceQuery(config, query, variables) {
'x-api-key': config['commerce-x-api-key'],
};

const apiCall = new URL('https://catalog-service.adobe.io/graphql');
const apiCall = new URL(config['commerce-endpoint']);
apiCall.searchParams.append('query', query.replace(/(?:\r\n|\r|\n|\t|[\s]{4})/g, ' ')
.replace(/\s\s+/g, ' '));
apiCall.searchParams.append('variables', variables ? JSON.stringify(variables) : null);
Expand Down Expand Up @@ -49,7 +51,7 @@ const getProducts = async (config, pageNumber) => {
if (response && response.productSearch) {
const products = await Promise.all(response.productSearch.items.map(async (item) => {
const {
url,
urlKey,
sku,
metaDescription,
name,
Expand All @@ -59,8 +61,6 @@ const getProducts = async (config, pageNumber) => {
shortDescription,
} = item.productView;
const { url: imageUrl } = item.product.image ?? {};
const productUrl = new URL(url);
const urlKey = productUrl.pathname.substring(1, productUrl.pathname.length - 5);

let baseImageUrl = imageUrl;
if (baseImageUrl.startsWith('//')) {
Expand All @@ -83,7 +83,6 @@ const getProducts = async (config, pageNumber) => {
return {
productView: {
...item.productView,
urlKey,
image: baseImageUrl,
path: `/products/${urlKey}/${sku.toLowerCase()}`,
meta_keyword: (metaKeyword !== null) ? metaKeyword : '',
Expand Down Expand Up @@ -121,15 +120,39 @@ const getProducts = async (config, pageNumber) => {
const products = await getProducts(config, 1);

const data = [
['URL', 'keywords', 'title', 'og:title', 'description', 'og:description', 'og:image', 'og:image:secure_url', 'twitter:image'],
[
'URL',
'title',
'description',
'keywords',
'og:type',
'og:title',
'og:description',
'og:url',
'og:image',
'og:image:secure_url',
'twitter:card',
'twitter:title',
'twitter:image'
],
];
products.forEach(({ productView: metaData }) => {
data.push(
[metaData.path, metaData.meta_keyword,
metaData.meta_title, metaData.meta_title,
metaData.meta_description, metaData.meta_description,
metaData['og:image'], metaData['og:image:secure_url'],
metaData['twitter:image']],
[
metaData.path, // URL
metaData.meta_title, // title
metaData.meta_description, // description
metaData.meta_keyword, // keywords
'og:product', // og:type
metaData.meta_title, // og:title
metaData.meta_description, // og:description
`${basePath}${metaData.path}`, // og:url
metaData['og:image'], // og:image
metaData['og:image:secure_url'], // og:image:secure_url
metaData.meta_description, // twitter:card
metaData.meta_title, // twitter:title
metaData['twitter:image'], // twitter:image
],
);
});

Expand Down
2 changes: 1 addition & 1 deletion tools/pdp-metadata/queries/products.graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default `query productSearch($currentPage: Int = 1) {
__typename
sku
name
url
urlKey
shortDescription
description
metaDescription
Expand Down

0 comments on commit 609421e

Please sign in to comment.