创建自定义Cloud Service creating-a-custom-cloud-service

CAUTION
AEM 6.4已结束扩展支持,本文档将不再更新。 有关更多详细信息,请参阅 技术支助期. 查找支持的版本 此处.

默认的Cloud Services集可以使用自定义Cloud Service类型进行扩展。 这允许您以结构化方式将自定义标记注入页面。 这将主要用于第三方分析提供商,例如Google Analytics、Chartbeat等。 Cloud Services从父页面继承到子页面,能够在任何级别中断继承。

NOTE
此创建新Cloud Service的分步指南是使用Google Analytics的示例。 所有内容可能不适用于您的用例。
  1. 在CRXDE Lite中,在 /apps:

    • 名称: acs
    • 类型: nt:folder
  2. 在下创建新节点 /apps/acs:

    • 名称: analytics
    • 类型: sling:Folder
  3. 在下创建2个新节点 /apps/acs/analytics:

    • 名称:组件
    • 类型: sling:Folder

    • 名称:模板
    • 类型: sling:Folder
  4. 右键单击 /apps/acs/analytics/components. 选择 创建…… 后跟 创建组件…… 打开的对话框允许您指定:

    • 标签: googleanalyticspage
    • 标题: Google Analytics Page
    • 超级类型: cq/cloudserviceconfigs/components/configpage
    • : .hidden
  5. 单击 下一个 两次,指定:

    • 允许的父项: acs/analytics/templates/googleanalytics

    单击 下一个 两次单击 确定.

  6. 将资产添加到 googleanalyticspage:

    • 名称: cq:defaultView
    • 值: html
  7. 创建名为的新文件 content.jsp/apps/acs/analytics/components/googleanalyticspage,其中包含以下内容:

    code language-xml
    <%@page contentType="text/html"
                pageEncoding="utf-8"%><%
    %><%@include file="/libs/foundation/global.jsp"%><div>
    
    <div>
        <h3>Google Analytics Settings</h3>
        <ul>
            <li><div class="li-bullet"><strong>accountID: </strong><br><%= xssAPI.encodeForHTML(properties.get("accountID", "")) %></div></li>
        </ul>
    </div>
    
  8. 在下创建新节点 /apps/acs/analytics/components/googleanalyticspage/:

    • 名称: dialog

    • 类型: cq:Dialog

    • 属性:

      • 名称: title
      • 类型: String
      • : Google Analytics Config
      • 名称: xtype
      • 类型: String
      • : dialog
  9. 在下创建新节点 /apps/acs/analytics/components/googleanalyticspage/dialog:

    • 名称: items

    • 类型: cq:Widget

    • 属性:

      • 名称: xtype
      • 类型: String
      • : tabpanel
  10. 在下创建新节点 /apps/acs/analytics/components/googleanalyticspage/dialog/items:

    • 名称: items
    • 类型: cq:WidgetCollection
  11. 在下创建新节点 /apps/acs/analytics/components/googleanalyticspage/dialog/items/items:

    • 名称:tab1

    • 类型: cq:Panel

    • 属性:

      • 名称: title
      • 类型: String
      • : Config
  12. 在下创建新节点 /apps/acs/analytics/components/googleanalyticspage/dialog/items/items/tab1:

    • 名称:项目

    • 类型: nt:unstructured

    • 属性:

      • 名称: fieldLabel

      • 类型:字符串

      • :帐户ID

      • 名称: fieldDescription

      • 类型: String

      • : The account ID assigned by Google. Usually in the form UA-NNNNNN-N

      • 名称: name

      • 类型: String

      • : ./accountID

      • 名称: validateOnBlur

      • 类型: String

      • : true

      • 名称: xtype

      • 类型: String

      • : textfield

  13. 复制 /libs/cq/cloudserviceconfigs/components/configpage/body.jsp to /apps/acs/analytics/components/googleanalyticspage/body.jsp 更改 libs to apps 在第34行,并使第79行上的脚本引用成为完全限定的路径。

  14. 在下创建新模板 /apps/acs/analytics/templates/:

    • with 资源类型 = acs/analytics/components/googleanalyticspage
    • with 标签 = googleanalytics
    • with 标题= Google Analytics Configuration
    • with allowedPath = /etc/cloudservices/googleanalytics(/.*)?
    • with allowedChildren = /apps/acs/analytics/templates/googleanalytics
    • with sling:resourceSuperType = cq/cloudserviceconfigs/templates/configpage (在模板节点上,而不是jcr:content节点上)
    • with cq:designPath = /etc/designs/cloudservices/googleanalytics (在jcr:content上)
  15. 创建新组件: /apps/acs/analytics/components/googleanalytics.

    将以下内容添加到 googleanalytics.jsp:

    code language-xml
    <%@page import="org.apache.sling.api.resource.Resource,
                    org.apache.sling.api.resource.ValueMap,
                    org.apache.sling.api.resource.ResourceUtil,
                    com.day.cq.wcm.webservicesupport.Configuration,
                    com.day.cq.wcm.webservicesupport.ConfigurationManager" %>
    <%@include file="/libs/foundation/global.jsp" %><%
    
    String[] services = pageProperties.getInherited("cq:cloudserviceconfigs", new String[]{});
    ConfigurationManager cfgMgr = resource.getResourceResolver().adaptTo(ConfigurationManager.class);
    if(cfgMgr != null) {
        String accountID = null;
        Configuration cfg = cfgMgr.getConfiguration("googleanalytics", services);
        if(cfg != null) {
            accountID = cfg.get("accountID", null);
        }
    
        if(accountID != null) {
        %>
    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', '<%= accountID %>']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    
    </script><%
        }
    }
    %>
    

    这应该根据配置属性输出自定义标记。

  16. 导航到 http://localhost:4502/miscadmin#/etc/cloudservices 并创建新页面:

    • 标题: Google Analytics
    • 名称: googleanalytics

    返回CRXDE Lite,然后返回 /etc/cloudservices/googleanalytics,将以下资产添加到 jcr:content:

    • 名称: componentReference
    • 类型: String
    • : acs/analytics/components/googleanalytics
  17. 导航到新创建的服务页面( http://localhost:4502/etc/cloudservices/googleanalytics.html),然后单击 + 要创建新配置,请执行以下操作:

    • 父配置: /etc/cloudservices/googleanalytics
    • 标题: My First GA Config

    选择 Google Analytics配置 单击 创建.

  18. 输入 帐户ID,例如 AA-11111111-1. 单击​ 确定

  19. 导航到页面,并在页面属性中的 Cloud Services 选项卡。

  20. 页面将在其中添加自定义标记。

recommendation-more-help
2315f3f5-cb4a-4530-9999-30c8319c520e