Monday, December 24, 2018

How to enable the authors to override or add new meta tags to the pages? - Adobe Experience Manager(AEM)

How to enable the authors to override or add new meta tags to the pages? - Adobe Experience Manager(AEM)


The custom meta tags in AEM are predefined in components with required values - the values can be dynamically taken from properties or external system in case of eCommerce websites,  the authors will not be able to overwrite(the values can be changed but some cases we may required the value should be changed only for meta tags but not in the source) or add new meta tags during the page authoring.

Meta tags in page components:

meta-tags-aem

meta-tags-aem


This post will explain the approach to enable the authoring capability to override the meta tags defined in the page components and also to add new meta tags to the pages.

Enabling the meta tag management(overridden the existing meta tags and add new meta tags ) for author:

Override the Page Proprieties to add additional tab to define meta tags:


Create a node cq:dialog under page component(e.g /apps/weretail/components/structure/page/)

Name  : cq:dialog
Type : nt:unstructured

Property

sling:resourceType cq/gui/components/authoring/dialog

Create a node content under /apps/weretail/components/structure/page/cq:dialog

Name  : content
Type : nt:unstructured

Create a node items under /apps/weretail/components/structure/page/cq:dialog/content

Name  : items
Type : nt:unstructured

Create a node tabs under /apps/weretail/components/structure/page/cq:dialog/content/items

Name  : tabs
Type : nt:unstructured

Create a node items under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs

Name  : items
Type : nt:unstructured

Create a node metatags under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items

Name  : metatags
Type : nt:unstructured

Properties

jcr:title Meta Tags
sling:resourceType granite/ui/components/coral/foundation/fixedcolumns

Create a node items under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags

Name  : items
Type : nt:unstructured

Create a node column under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags

Name  : column
Type : nt:unstructured

Properties
sling:resourceType granite/ui/components/coral/foundation/container

Create a node items under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column

Name  : items
Type : nt:unstructured

Create a node overridemetatags  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items

Name  : overridemetatags
Type : nt:unstructured

Properties
composite true
fieldLabel Override Meta tags
sling:resourceType granite/ui/components/coral/foundation/form/multifield

Create a node newmetatags  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items

Name  : newmetatags
Type : nt:unstructured

Properties
composite true
fieldLabel New Meta tags
sling:resourceType granite/ui/components/coral/foundation/form/multifield

Create a node field  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/overridemetatags

Name  : field
Type : nt:unstructured

Properties
name ./overRideMetadata
sling:resourceType granite/ui/components/coral/foundation/container

Create a node items  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/overridemetatags/field

Name  : items
Type : nt:unstructured

Create a node metaname  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/overridemetatags/field/items

Name  : metaname
Type : nt:unstructured

Properties
name ./metaname
fieldLabel Meta tag Name
sling:resourceType granite/ui/components/coral/foundation/form/textfield

Create a node metavalue  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/overridemetatags/field/items

Name  : metavalue
Type : nt:unstructured

Properties
name ./metavalue
fieldLabel Meta tag Value
sling:resourceType granite/ui/components/coral/foundation/form/textfield

Create a node field  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/newmetatags

Name  : field
Type : nt:unstructured

Properties
name ./newMetadata
sling:resourceType granite/ui/components/coral/foundation/container

Create a node items  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/newmetatags/field

Name  : items
Type : nt:unstructured

Create a node metaname  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/newmetatags/field/items

Name  : metaname
Type : nt:unstructured

Properties
name ./metaname
fieldLabel Meta tag Name
sling:resourceType granite/ui/components/coral/foundation/form/textfield

Create a node metavalue  under /apps/weretail/components/structure/page/cq:dialog/content/items/tabs/items/metatags/column/items/newmetatags/field/items

Name  : metavalue
Type : nt:unstructured

Properties
name ./metavalue
fieldLabel Meta tag Value
sling:resourceType granite/ui/components/coral/foundation/form/textfield

Saturday, December 15, 2018

Different approaches to dynamically include custom scripts into websites - Adobe Experience Manager(AEM)


The standard way to include a client-side library (that is, a JS or CSS file) in the HTML of a page is simply to include a <script> or <link> tag for that page, containing the path to the JS or CSS files. For example

...
<head>...
<script type="text/javascript" src="/etc.clientlibs/clientlibs/test.js"></script>
...
</head>
...

Even the inline scripts can be added to the page

<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>


While these approaches work in AEM by adding the client-side library as a link or inline to the page components, AEM provides Client-side Library Folders, which allow you to store your client-side code — JavaScript and CSS in the repository, organize it into categories, and define when and how each category of code is to be served to the client. The client-side library system then takes care of producing the correct links in your final webpage to load the correct code.

<sly data-sly-use.clientlib="/libs/granite/sightly/templates/clientlib.html"
data-sly-call="${clientlib.all @ categories=['myCategory1', 'myCategory2']}"/>

The generated HTML page contains the reference to the final script URL

....
<head>
...
<script type="text/javascript" src="/etc.clientlibs/clientlibs/test.js"></script>
...
</head>
...

Sometimes we may need to include scripts dynamically into the websites without changing the code, this tutorial will explain the different approaches to include the scripts dynamically in the websites without performing the code changes.

Tag Manager:


Tag management software simplifies and unifies the management and maintenance of tags — A tag is a short snippet of code across, all digital properties. Inject the custom scripts remotely by enabling container on the pages, this will provide better management of tags without changing the code.

You can use any one of the tag managers in the market to include the custom scripts into the page. GTM and Adobe Launch are the two popular tag management system in the market.

GTM(Google Tag Manager) — Refer to the below URL for more details on injecting the custom script through GTM.


Adobe Launch- Refer to the below URL for more details on injecting the custom script through Adobe Launch.


AEM Custom Component:


In this approach define a custom component and include that wherever required in the page to inject the custom scripts and HTML dynamically.

Let us now enable a custom component e.g OpenHTML that will include the scripts to the page.

The component should be enabled with the required dialog to configure the custom scripts and HTML


Add the following content into openhtml.html

<sly data-sly-test.editor="${wcmmode.edit}">
<p>
Open HTML
</p>
</sly><!-- Open HTML component -->
${properties.openhtml @ context='unsafe'}
<!-- end of Open HTML component -->


Drag and drop the component to the parsys on the required website and configure the script — make sure the script/HTML is valid and all the tags are closed.

In Edit mode the place holder string “Open Html” is displayed


Now author the required scripts and HTML— ensure the script/HTML is valid otherwise the page may break


Now the HTML content and the script is included into the website page


AEM Cloud configuration:


This approach uses the custom AEM Cloud Configuration to include the dynamic scripts — header and footer to the website

Define a custom Cloud configuration to enable the header and footer scripts, attach the cloud configuration to the required websites to inject the custom dynamic scripts. Even you should be able to manage the scripts through page properties or through an external configuration page.

Refer to the following URL for the sample cloud service that enables the configuration option for header and footer scripts.

https://github.com/techforum-repo/youttubedata/tree/master/aem/customcloudservice

Add the below line to the header file(e.g /apps/wknd/components/structure/page/customheaderlibs.html) of your page rendering component — this will display the header script configured in the generic-script cloud configuration to head section

<sly data-sly-include="/apps/utilities/cloudserviceconfigs/components/servicelibs/serviceheaderlibs.jsp" />

Add the below line to the footer file(/apps/wknd/components/structure/page/customfooterlibs.html) of your page rendering component — this will display the footer script configured in the generic-script cloud configuration to the end of the body section

<sly data-sly-include="/apps/utilities/cloudserviceconfigs/components/servicelibs/servicefooterlibs.jsp" />

Create a service user with the name “script-service” and enable the read access for “/apps/utilities” and “/etc/cloudservices/generic-script”


Add an entry in “Apache Sling Service User Mapper Service Amendment” for “script-service” — “customcloudservice.core:script-service=script-service”


Create a configuration page of type “generic-script” e.g sample-script through http://localhost:4503/libs/cq/core/content/tools/cloudservices.html and configure the required head and footer scripts.




Configure the cloud configuration in parent page properties


Now the website displays the header and footer scripts



This covers some of the feasible options to dynamically include the scripts into the pages without modifying the code, even some of the other options e.g configure through separate site-specific configuration pages, enable through page properties dialog, etc can be used based on the requirement.