變更內容搜尋網頁組件顯示範本,並使用 Windows PowerShell 在 SharePoint Server 中開始進行使用情況分析Change the Content Search Web Part display template and use Windows PowerShell to start Usage analytics in SharePoint Server

摘要: 了解如何變更內容搜尋網頁組件顯示範本,並使用 Microsoft PowerShell 在 SharePoint Server 2016 和 SharePoint Server 2013 中開始進行使用情況分析。Summary: Learn how to change the Content Search Web Part display template and use Microsoft PowerShell to start Usage analytics in SharePoint Server 2016 and SharePoint Server 2013.

注意

本系列中的範例是根據內部部署的 SharePoint Server 部署。The examples in this series are based on an on-premises SharePoint Server deployment.

變更 UsageAnalyticsID Managed 屬性的對應Change the mapping of the UsageAnalyticsID managed property

在 Contoso 網站中,我們建議每個產品群組有一項產品,也就是我們希望「使用情況分析」略過產品色彩。這表示,我們的建議必須在「群組編號」** 上計算。我們可以這麼做,因為「群組編號」** 屬於項目詳細資料頁面上易記 URL (FURL) 的一部分 (請參閱<階段 10:設定內容搜尋網頁組件的 SharePoint Server 中的目錄項目頁面上的查詢>)。In our Contoso website, we want to recommend one product per product group, that is, we want Usage analytics to ignore the product color. This means that our recommendations must be calculated on Group Number. We can do this, because Group Number is part of the friendly URL (FURL) on our item detail page (see Stage 10: Configure the query in a Content Search Web Part on a catalog item page).

在先前的部落格文章中,我們提到了用來指定該如何計算個別目錄項目間建議數的 Managed 屬性為 UsageAnalyticsID** (請參閱<關於 UsageAnalyticsID Managed 屬性>)。所以,為了讓「使用情況分析」在「群組編號」** 上進行其計算,我們必須變更 UsageAnalyticsID** 屬性的對應。以下是具體做法:In the previous blog post, we told you that managed property that’s used to specify how recommendations between individual catalog items should be calculated is UsageAnalyticsID (see About the UsageAnalyticsID Managed Property). So, for Usage analytics to do its calculation on Group Number, we must change the mapping of the UsageAnalyticsID property. Here’s how you do that:

重要

您必須在製作網站上變更屬性對應。You have to change the property mapping on the authoring site.

  1. 在製作網站上,移至 [網站設定] --> [搜尋結構描述]****。On your authoring site, go to Site settings --> Search Schema.

    搜尋結構描述

  2. 在 [Managed 屬性]**** 頁面的 [Managed 屬性]**** 欄位中,輸入 UsageAnalyticsID,然後選取箭號按鈕。On the **Managed Properties page, in the Managed property field, type UsageAnalyticsID, and then select the arrow button.

    搜尋流量分析 MP

  3. 從 [屬性名稱]**** 欄位中,選取 [編輯/對應屬性]*From the *Property Name field, select Edit/Map Property.

    編輯屬性

  4. 在 [編輯 Managed 屬性]**** 頁面上,選取 [新增對應]*。請注意,根據預設,此屬性會對應到編目屬性 ows_ProductCatalogItemNumber*。On the **Edit Managed Property page, select Add a Mapping. Notice that by default, this property is mapped to the crawled property ows_ProductCatalogItemNumber.

    新增 A 對應

  5. 在 [選取編目屬性]**** 對話方塊中,使用 [搜尋編目屬性名稱]**** 欄位來搜尋您要對應到此 Managed 屬性的編目屬性。In the Crawled property selection dialog box, use the Search for crawled property name field to search for the crawled property that you want to map to this managed property.

    在 Contoso 案例中,我們想要對應名為 「群組編號」** 的網站欄。編目屬性不含空格。因此,去掉空格,輸入 GroupNumber,然後選取 [尋找]In our Contoso scenario, we want to map the site column called Group Number. Crawled properties don’t contain spaces. Therefore, exclude the space, enter GroupNumber and select **Find.

    輸入群組號碼並選取 [尋找]

找到兩個編目屬性。選取具有 ows_** 前置詞的編目屬性,然後選取 [確定]*Two crawled properties are found. Select the crawled property with the *ows_ prefix, and select **OK.

 ![Map Property](../media/OTCSP_MapProperty.png)

如果您因為找到兩個看似相同的編目屬性而感到困惑,您並不孤單。這有點微妙。From site column to managed property - What's up with that? 這篇文章說明編目和 Managed 屬性的命名慣例。如果您對於縮寫版本感興趣,如下所示:將編目屬性對應到 UsageAnalyticsID** Managed 屬性時,您應該選取具有 ows_**** 前置詞的編目屬性!If you are confused because two crawled properties that look about the same are found, you’re not alone. This is somewhat tricky. The article From site column to managed property - What's up with that? explains the naming convention for crawled and managed properties. If you are interested in an abbreviated version, here it is as follows: When mapping a crawled property to the UsageAnalyticsID managed property, you should select the crawled property with the ows_ prefix!

  1. 在 [編輯 Managed 屬性]**** 頁面上,選取 ows_ProductCatalogItemNumber** 編目屬性,然後選取 [移除對應]*On the *Edit Managed Property page, select the ows_ProductCatalogItemNumber crawled property, and then Remove Mapping.

    移除對應

  2. 請選取 [確定]**** 以儲存新的對應。Select OK to save the new mapping.

    重要

    只將一個編目屬性** 對應至 UsageAnalyticsID Managed** 屬性。如果您對應一個以上的編目屬性,「使用情況分析」計算將無法正常運作。Map only one crawled property to the UsageAnalyticsID managed property. If you map more than one crawled property, the Usage analytics calculation won’t work correctly.

    在您變更 UsageAnalyticsID** 屬性的對應之後,請進行目錄的完整編目,如<階段 4:設定搜尋並啟用在 SharePoint Server 中的目錄內容的編目>中所述。After you have changed the mapping of the UsageAnalyticsID managed property, do a full crawl of your catalog, as explained in Stage 4: Set up search and enable the crawling of your catalog content.

變更內容搜尋網頁組件顯示範本,以便正確記錄使用事件Change a Content Search Web Part display template so the usage events are logged correctly

在 Contoso 網站上,我們使用內容搜尋網頁組件 (CSWP),在目錄項目頁面上顯示項目,如階段 10: 設定內容搜尋網頁組件的 SharePoint Server 中的目錄項目頁面上的查詢中所述。根據預設,CSWP 不會記錄使用事件。若要讓 CSWP 能夠記錄使用事件,我們必須變更 CSWP 所使用的顯示範本。以下是您必須執行的作業:On our Contoso site, we use a Content Search Web Part (CSWP) to display items on the catalog item page, as explained in Stage 10: Configure the query in a Content Search Web Part on a catalog item page in SharePoint Server. By default, the CSWP does not log usage events. To enable our CSWP to log usage events we must change the display template the CSWP is using. Here's what you have to do:

  1. 如何將對應網路磁碟機中,開啟您已套用到 CSWP 的顯示範本。In your How to map your network drive, open the display template that you have applied to your CSWP.

  2. ManagedPropertyMapping 元素中,新增下列兩個屬性:In the ManagedPropertyMapping element, add the following two properties:

    'Original Path'{Original Path}:'OriginalPath',
    'SiteID'{SiteID}:'SiteID',
    

    兩個 MP

  3. 將下列 Java 指令碼新增於顯示範本的 HTML 組件正上方:Add the following Java-Script just above the HTML part of your display template:

    //Log Views usage event on URL of catalog item
       window.LogViewsToEventStore = function(url, site)
       {    
           SP.SOD.executeFunc("sp.js", "SP.ClientContext", function()
           {
               var spClientContext = SP.ClientContext.get_current();
               if(!$isNull(spClientContext))
               {
                       var spWeb = spClientContext.get_web();
                       var spUser = spWeb.get_currentUser();
                       var spScope = "{00000000-0000-0000-0000-000000000000}";
    
                       SP.Analytics.AnalyticsUsageEntry.logAnalyticsEvent2(spClientContext, 1, url, spScope, site, spUser);spClientContext.executeQueryAsync(null, null);                   
               }
           });
       };
    
       var originalPath = $getItemValue(ctx, "Original Path");
       var originalSite = $getItemValue(ctx, "SiteID");
       LogViewsToEventStore(originalPath.value, originalSite.value);
    

    檢視使用事件定義中,我們說明了使用事件的 EventTypeID。此指令碼中的值 1** 表示 Views** 使用事件的 EventTypeID。若要登入不同的使用事件,請將這個值替換成您要記錄之使用事件的 EventTypeID。In View the Usage Event Definitions we explained the EventTypeIDs for the usage events. The value 1 in this script represents the EventTypeID of the Views usage event. To log a different usage event, substitute this value with the EventTypeID of the usage event that you want to log.

    Java 指令碼

  4. 儲存檔案。Save the file.

您為何應該模擬 Views 使用事件的產生Why you should simulate the generation of Views usage events

既然 CSWP 可正確地記錄使用事件,下一個步驟就是實際產生使用事件。在我們的案例中,我們已將 CSWP 變更為記錄 Views。如果 Contoso 網站已「上線」,則訪客每次檢視網站上的項目時都會建立 Views 使用事件。但是,Contoso 網站只是一個示範網站。因此不會有任何訪客。Now that our CSWP can correctly log usage events, the next step is to actually generate usage events. In our case, we changed the CSWP to log Views. If the Contoso site was LIVE, visitors would create a Views usage event every time that they viewed an item on the website. But, the Contoso site is only a demo site. Therefore, it doesn’t have any visitors.

當您設定您的網站時,您很可能會先測試「使用情況分析」功能,再將該網站置於生產環境。若要測試「使用情況分析」功能,您必須產生使用事件。若要根據使用事件產生建議, 至少有 3 位使用者必須點選相同的項目When you set up your site, you'll most likely want to test the Usage analytics feature before you put it into production. To be able to test the Usage analytics feature, you'll have to generate usage events. To generate recommendations based on usage events, a minimum of three users have to click the same items .

模擬 Views** 使用事件的產生時,並沒有單一正確方法。若要產生 Contoso 網站的 Views** 使用事件,您可以邀請同事參加「點選派對」(必要時以糖果收買他們)。若要確保產生建議,請提供每位使用者可點選的項目清單。如此一來,您可以確保至少有三個使用者點選相同的項目。There is no single correct way of simulating the generation of Views usage events. To generate Views usage events for the Contoso site, you may want to invite coworkers to a “click party” (bribe them with candy if it is necessary). To to make sure that that recommendations are generated, give each user a list of items to click. That way, you can to make sure that that at least three users click the same items.

以下是您可以提供給同事的指示範例:Here's an example of the instructions that you can give your coworkers:

按一下合作對象指示

執行「使用情況分析」時, SV Keyboard E10 將會產生 WWI Desktop PC2.30 M2300 的建議 (已檢視 WWI Desktop PC2.30 M2300 的人員也檢視了 SV Keyboard E10 ),而 WWI Desktop PC2.30 M2300 將會產生 SV Keyboard E10 的建議 (已檢視 SV Keyboard E10 的人員也檢視了 WWI Desktop PC2.30 M2300 )。When Usage analytics is run, SV Keyboard E10 will generate a recommendation for WWI Desktop PC2.30 M2300 (people who viewed WWI Desktop PC2.30 M2300 also viewed SV Keyboard E10 ), and WWI Desktop PC2.30 M2300 will generate a recommendation for SV Keyboard E10 (people who viewed SV Keyboard E10 also viewed WWI Desktop PC2.30 M2300 ).

執行 Microsoft PowerShell 指令碼,開始搜尋分析並將使用事件推送到事件存放區Run Microsoft PowerShell scripts to start search analytics and push usage events to the Event store

產生 Views** 使用事件之後,您有兩個有關如何繼續的選項。使用情況分析計時器作業會每隔 24 小時執行一次。如果您有耐心,您現在可以放鬆、回家,讓使用情況分析一整晚發揮其神奇作用。After you have generated Views usage events, you have two options on how to continue. The Usage analytics timer job runs one time every 24 hours. If you are patient, you can now relax, go home, and let Usage analytics work its magic overnight.

另一方面,如果您現在就想要結果,您可以使用一些 Microsoft PowerShell 指令碼來加快程序。以下是您必須執行的作業:On the other hand, if you want results now, you can use some Microsoft PowerShell scripts to speed up the process. Here's what you have to do:

  1. 確認您符合最低權限需求:Verify that you meet the minimum permission requirements.

  2. 在安裝 SharePoint Server 的伺服器上,以系統管理員身分開啟 SharePoint 2013 管理命令介面On the server where SharePoint Server is installed, open the SharePoint 2013 Management Shell as an Administrator.

    以系統管理員身分執行

  3. 在 Microsoft PowerShell 命令提示字元處輸入下列命令,開始進行搜尋分析。使用情況分析會使用搜尋分析的輸出,對應使用事件與搜尋索引中的實際項目。At the Microsoft PowerShell command prompt, type the following commands to start Search analytics. The output from Search analytics is used by Usage analytics to map usage events against the actual items in the search index.

    $a = Get-SPTimerJob -Type Microsoft.Office.Server.Search.Analytics.AnalyticsJobDefinition
    $sa = $a.GetAnalysis("Microsoft.Office.Server.Search.Analytics.SearchAnalyticsJob")
    $sa.StartAnalysis()
    
  4. 請等候搜尋分析作業完成。若要檢查搜尋分析作業的狀態,請輸入下列命令:Wait for the search analytics job to finish. To check the status of the search analytics job, type the following command:

    $sa.GetAnalysisInfo()
    

    只要搜尋分析作業正在執行, State 就是 RunningAs long as the search analytics job is running, State is Running.

    SA 執行中

StateStoppedProgress100 時,搜尋分析作業便已完成。The search analytics job is finished when State is Stopped and Status is 100.

 ![SA Stopped](../media/OTCSP_SAstoped.png)
  1. 使用事件會每隔 10 分鐘新增到事件存放區。若要將使用事件推送到存放區,請輸入下列命令:The usage events are added to the Event store in a 10 minute interval. To push the usage events to the Event store, type the following commands:

    $tj = Get-SPTimerJob -Identity ("job-usage-log-file-import")
    $tj.RunNow()
    

檢視事件存放區中的使用事件View usage events in the Event store

將使用事件推送到事件存放區之後,您必須確認已正確記錄使用事件。若要這麼做,請在安裝 SharePoint Server 的電腦上,移至事件存放區。在大多數情況下,您可以在下列資料夾中找到事件存放區:After you have pushed the usage events into the Event store, you should verify that the usage events are recorded correctly. To do this, on the machine where SharePoint Server is installed, go to the Event store. In most cases, you can find the Event store in the following folder:

C:\Program Files\Microsoft Office Servers\15.0\Data\Office Server\Analytics_<GUID>\EventStoreC:\Program Files\Microsoft Office Servers\15.0\Data\Office Server\Analytics_<GUID>\EventStore

在事件存放區中,每天的使用事件會儲存在個別的資料夾中。在我們的案例中,我們可看到已新增一個資料夾。In the Event store, the usage events of each day are stored in a separate folder. In our scenario, we can see that a folder was added.

事件儲存區尋找工具

在這個資料夾中,您會看到一些文字檔案。這些檔案包含我們的使用事件。請注意,所有檔案名稱都是以 1_ 開頭。In this folder, you'll see some text files. These files contain our usage events. Notice that all file names start with 1_.

事件記錄檔

此數字是檔案中記錄之使用事件的 EventTypeID。請記住,1**** 是 Views** 使用事件的 EventTypeID (請參閱<檢視使用事件定義>)。此時,我們所記錄的唯一事件是 Views** 事件。所以這是個好兆頭,我們目前執行的動作正確無誤。This number is the EventTypeID of the usage event that is logged in the file. Remember, 1 is the EventTypeID of the Views usage event (see View the Usage Event Definitions). At this point the only usage event we are logging is the Views event. So this is a good sign that we are doing things right.

在文字編輯器 (例如 Notepad++) 中開啟其中一個檔案。此檔案包含大量資訊,但您真的只要查看兩件事:Open one of the files in a text editor, for example Notepad++. This file contains lots of information, but you should really only be looking for two things:

  • 確認已正確記錄使用事件。Verify that the usage events are logged correctly.

  • 確認不同的使用者已產生此使用事件。Verify that different users have generated the usage event.

關於流量分析中的跨網站發佈案例中,我們已說明為了讓使用情況分析得以運作,則必須將使用事件記錄在項目的 URL 中。在事件存放區檔案中,您會看到許多 URL。尋找結尾是 dispform.aspx?id= 且後面接著一個數字的 URL。在這個檔案的 Contoso 版本中,我們看到有許多具有這類 URL 的項目。In About Usage analytics in a cross-site publishing scenario we explained that for Usage analytics to work, the usage event must be recorded on the URL of the item. In the Event store file, you'll see many URLs. Look for URLs that end in dispform.aspx?id= followed by a number. In our Contoso version of this file, we see there are many entries with such URLs.

項目 URL

這是個好兆頭,因為這表示目前正確記錄使用事件。若要確認 URL 實際屬於我們的其中一項目錄項目,請從此檔案複製其中一個 URL 並將它貼到瀏覽器中。This is a very good sign, because it means that the usage events are being recorded correctly. To verify that the URLs actually belong to one of our catalog items, copy one of the URLs from this file, and paste it into your browser.

驗證項目 URL

若要確認不同的使用者已產生此使用事件,請查看檔案的第三欄。在我們的案例中,我們可以看到我們至少有三個使用者識別碼。To verify that different users have generated the usage event, look in the third column of the file. In our scenario, we can see that we have at least three user IDs.

三個使用者 ID

既然我們已確認使用事件正確記錄,您可能會認為我們已準備好執行使用情況分析作業。不過別忘了,使用 Microsoft PowerShell 開始進行使用情況分析,我們實際是開始執行計時器作業。Now that we have verified that the usage events are correctly logged, you might be tempted to think that we are ready to run the Usage analytics job. But remember, by using Microsoft PowerShell to start Usage analytics, we are actually kick-starting timer jobs.

使用情況分析計時器作業開始時,它會取得昨天的使用事件進行處理。因為我們想要處理今天的檔案,所以我們將使用一個簡單的手法,讓使用情況分析得以處理正確的檔案。When the Usage analytics timer job starts, it'll take the usage events from yesterday and process them. Since we want to process the files from today, we'll use a simple trick so that the correct files can be processed by Usage analytics.

在您使用 Windows PowerShell 開始進行使用情況分析前準備使用事件檔案Prepare usage events files before you start Usage analytics with Windows PowerShell

  1. 在 EventStore 資料夾中,建立名為 myevents** 的資料夾。In your EventStore folder, create a folder named myevents.

    我的事件

  2. 將您要使用情況分析處理的使用事件檔案複製到您的 myevents** 資料夾中。在 Contoso 案例中,將檢視事件存放區中的使用事件資料夾中的所有檔案複製到 myevents** 中。Copy the usage event files that you want Usage analytics to process into your myevents folder. In our Contoso scenario, copy all files from the 20131206 folder into myevents.

    已複製我的事件

  3. 在 myevents** 資料夾上按一下滑鼠右鍵,然後選取 [屬性]*Right-click your *myevents folder and select **Properties.

    Windows 檔案總管中的內容位置

  4. 在 [屬性]**** 區段中,選取 [唯讀 (僅適用於資料夾中的檔案)]*,然後按一下 [確定]In the **Attributes* section, select Read-only (Only applies to files in folder), and then click OK.

    設定為唯讀

  5. 在 [確認屬性變更]**** 對話方塊中,選取 [將變更套用到這個資料夾、子資料夾和檔案]*,然後按一下 [確定]In the **Confirm Attribute Changes* dialog box, select Apply changes to this folder, subfolder and files and then click OK.

    套用至資料夾

    您現在已準備好開始進行使用情況分析作業。Now you are ready to start the Usage analytics job.

使用 Microsoft PowerShell 開始進行使用情況分析作業Start the Usage analytics job with Microsoft PowerShell

  1. 在 Microsoft PowerShell 命令提示字元處,輸入下列命令:At the Microsoft PowerShell command prompt, type the following commands:

    $tj = get-sptimerjob -type microsoft.office.server.search.analytics.usageanalyticsjobdefinition 
    $tj.DisableTimerJobSchedule() 
    $tj.StartAnalysis("\\<hostname>\Analytics_<guid>\EventStore\myevents") 
    $tj.EnableTimerJobSchedule()
    

    請注意,一個命令包含兩個預留位置:主機名稱** 和 guid**。主機名稱是安裝 SharePoint_Server 的伺服器名稱。您可以在 EventStore 的檔案路徑中看到 guid。Notice that one command contains two placeholders: host name and guid. The host name is the name of the server where SharePoint_Server is installed. You can see the guid in the file path of your EventStore.

    GUID

    執行 UA

  2. 輸入下列命令,以檢查使用情況分析作業的狀態:Check the status of the Usage analytics job by entering the following command:

    $tj.GetAnalysisInfo()
    

    StateStoppedProgress100 時,使用情況分析作業便已完成。The Usage analytics job is finished when State is Stopped and Status is 100.

    已停止 UA

既然使用情況分析已處理使用事件,下一個步驟就是在我們的發佈網站上顯示分析結果。為了這麼做,我們將新增並設定兩個網頁組件。Now that Usage analytics have processed the usage events, the next step is to display the results of the analysis on our Publishing site. To do that, we'll add and configure two Web Parts.

本系列中的下一篇文章Next article in this series

Add and configure the Recommended Items and Popular Items Web Part in SharePoint ServerAdd and configure the Recommended Items and Popular Items Web Part in SharePoint Server