diff --git a/packages/marko-web-gtm/components/context/marko.json b/packages/marko-web-gtm/components/context/marko.json index ce189fdb3..637a587da 100644 --- a/packages/marko-web-gtm/components/context/marko.json +++ b/packages/marko-web-gtm/components/context/marko.json @@ -34,5 +34,11 @@ "@id": "string", "@builder": "function", "@query-fragment": "expression" + }, + "": { + "template": "./native-x-story.marko", + "@type": "string", + "@obj": "object", + "@builder": "function" } } diff --git a/packages/marko-web-gtm/components/context/native-x-story.marko b/packages/marko-web-gtm/components/context/native-x-story.marko new file mode 100644 index 000000000..f1e64a005 --- /dev/null +++ b/packages/marko-web-gtm/components/context/native-x-story.marko @@ -0,0 +1,9 @@ +import { isFunction, warn } from "@parameter1/base-cms-utils"; +import { nativeXStoryBuilder } from "../../context"; + +$ const { req } = out.global; +$ const builder = isFunction(input.builder) ? input.builder : nativeXStoryBuilder; +$ const type = input.type || "content"; + +$ const context = builder({ type, obj: input.obj, req }); +<${input.renderBody} context=context /> diff --git a/packages/marko-web-gtm/context/index.js b/packages/marko-web-gtm/context/index.js index 88da9c451..cc07f9ec4 100644 --- a/packages/marko-web-gtm/context/index.js +++ b/packages/marko-web-gtm/context/index.js @@ -4,6 +4,7 @@ const websiteSectionBuilder = require('./website-section'); const dynamicPageBuilder = require('./dynamic-page'); const magazineIssueBuilder = require('./magazine-issue'); const magazinePublicationBuilder = require('./magazine-publication'); +const nativeXStoryBuilder = require('./native-x-story'); module.exports = { defaultBuilder, @@ -12,4 +13,5 @@ module.exports = { dynamicPageBuilder, magazineIssueBuilder, magazinePublicationBuilder, + nativeXStoryBuilder, }; diff --git a/packages/marko-web-gtm/context/native-x-story.js b/packages/marko-web-gtm/context/native-x-story.js new file mode 100644 index 000000000..655f74181 --- /dev/null +++ b/packages/marko-web-gtm/context/native-x-story.js @@ -0,0 +1,37 @@ +const { getAsObject, getAsArray, get } = require('@parameter1/base-cms-object-path'); +const { asObject } = require('@parameter1/base-cms-utils'); +const buildQueryString = require('../utils/build-query-string'); + +module.exports = ({ type, obj, req }) => { + const content = asObject(obj); + const company = getAsObject(content, 'company'); + const section = getAsObject(content, 'primarySection'); + const hierarchy = getAsArray(section, 'hierarchy').map((s) => ({ + id: s.id, + name: s.name, + alias: s.alias, + })); + return { + page_type: type, + canonical_path: get(content, 'siteContext.path'), + query_string: buildQueryString({ req }), + content: { + id: content.id, + type: content.type, + name: content.name, + published: content.published ? new Date(content.published).toISOString() : undefined, + labels: getAsArray(content, 'labels'), + }, + company: { + id: company.id, + name: company.name, + }, + section: { + id: section.id, + name: section.name, + alias: section.alias, + fullName: section.fullName, + }, + section_hierarchy: hierarchy, + }; +};