カスタムクラウドサービスの作成

最終更新日: 2023-11-07
  • トピック:
  • Developing
    このトピックの詳細を表示
  • 作成対象:
  • Developer

デフォルトのCloud Servicesセットは、カスタムCloud Serviceタイプで拡張できます。 これにより、構造化された方法でページにカスタムマークアップを挿入できます。 これは主に、Google Analytics、Chartbeat など、サードパーティの分析プロバイダーに使用されます。 Cloud Services は、親ページから子ページに継承されますが、任意のレベルで継承を中断できます。

メモ

このステップバイステップガイドでは、Cloud Serviceを使用した例を示します。Google Analytics すべてがユースケースに当てはまらない場合があります。

  1. CRXDE Liteで、以下にノードを作成します。 /apps:

    • 名前acs
    • nt:folder
  2. の下にノードを作成します。 /apps/acs:

    • 名前analytics
    • sling:Folder
  3. の下に 2 つのノードを作成します。 /apps/acs/analytics:

    • 名前:components
    • sling:Folder

    および

    • 名前:templates
    • タイプsling:Folder
  4. 右クリック /apps/acs/analytics/components. 選択 作成… 続いて コンポーネントを作成… 開くダイアログでは、次の項目を指定できます。

    • ラベルgoogleanalyticspage
    • タイトルGoogle Analytics Page
    • スーパータイプcq/cloudserviceconfigs/components/configpage
    • グループ.hidden
  5. 次へ」を 2 回クリックして、次の項目を指定します。

    • 許可された親: acs/analytics/templates/googleanalytics

    次へ」を 2 回クリックして、「OK」をクリックします。

  6. プロパティを「googleanalyticspage」に追加します。

    • 名前: cq:defaultView
    • 値: html
  7. という名前のファイルを作成します。 content.jsp under /apps/acs/analytics/components/googleanalyticspage(次の内容)

    <%@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:

    • 名前:items

    • 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/apps/acs/analytics/components/googleanalyticspage/body.jsp にコピーして、34 行目の libsapps に変更し、79 行目のスクリプト参照を完全修飾パスにします。

  14. の下にテンプレートを作成します。 /apps/acs/analytics/templates/:

    • Resource Type = acs/analytics/components/googleanalyticspage
    • Label = googleanalytics
    • Title= Google Analytics Configuration
    • allowedPath = /etc/cloudservices/googleanalytics(/.*)?
    • allowedChildren = /apps/acs/analytics/templates/googleanalytics
    • sling:resourceSuperType = cq/cloudserviceconfigs/templates/configpage(jcr:content ノードではなくテンプレートノード)
    • cq:designPath = /etc/designs/cloudservices/googleanalytics(jcr:content)
  15. コンポーネントを作成します。 /apps/acs/analytics/components/googleanalytics.

    次の内容を googleanalytics.jsp に追加します。

    <%@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. 「OK」をクリックします。

  19. ページに移動し、新しく作成した設定をページプロパティの Cloud Service タブをクリックします。

  20. ページにはカスタムマークアップが追加されます。

このページ