Storing analytics tracking code with a site collection settings page

Though SharePoint includes some analytic features, many still prefer to use third-party web analytics providers such as Google Analytics. These analytics providers use a snippet of JavaScript code that is added to each page in most cases.

In this recipe, we will create a settings page to allow site collection administrators to store the JavaScript code on a per site collection basis.

Getting ready

We should have already created our Visual Studio project in the Creating a Visual Studio solution for custom delegate controls recipe of this chapter before starting this recipe.

How to do it...

Follow these steps to create a settings page for our tracking code:

  1. Open the project created in the Creating a Visual Studio solution for custom-delegate controls recipe of this chapter in Visual Studio.
  2. Add a new Application Page item to the subfolder we created in the Layouts mapped folder as shown in the following screenshot:
    How to do it...
  3. In the ASPX page, register the settings page user controls as follows:
    <%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="/_controltemplates/InputFormSection.ascx" %>
    <%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="/_controltemplates/InputFormControl.ascx" %>
    <%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="~/_controltemplates/ButtonSection.ascx" %>
  4. In the title content placeholders, add the title for our settings page as follows:
    <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Configure Analytics Tracking Code
    </asp:Content>
    <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    Configure Analytics Tracking Code
    </asp:Content>
  5. In the main content placeholder, add a <table> element to contain our settings page sections:
    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
      <table width="100%">
        <tr>
          <td>
          </td>
        </tr>
      </table>
    </asp:Content>

    The settings page user controls were designed to be placed inside a <table> element.

  6. In our <table> element, add an InputFormSection control with a TextBox control to input our analytics tracking code as follows:
    <wssuc:InputFormSection Title="Analytics Tracking Code" id="trackingCodeSection" runat="server" Description="The script block entered here will be rendered on each page in this SharePoint site.">
    <template_inputformcontrols>
      <wssuc:InputFormControl LabelText="Analytics Tracking Code Script Block" runat="server" LabelAssociatedControlId="txtScriptBlock">
      <Template_control>
        <asp:TextBox runat="server" Width="100%" ID="txtScriptBlock" TextMode="MultiLine" Height="300px" />
      </Template_control>
      </wssuc:InputFormControl>
    </template_inputformcontrols>
    </wssuc:InputFormSection>
  7. Add a ButtonSection control with a Button control to submit our analytics tracking code:
    <wssuc:ButtonSection runat="server">
    <Template_Buttons>
      <asp:Button UseSubmitBehavior="false" runat="server" class="ms-ButtonHeightWidth" OnClick="BtnSubmit_Click" Text="OK" id="BtnSaveChanges" accesskey="<%$Resources:wss,okbutton_accesskey%>"/>
    </Template_Buttons>
    </wssuc:ButtonSection>
  8. In the code-behind file of our application page, add a constant string for the name of the property we will save our analytics code within:
    private const string PropertyName = "CustomAnalyticsCode";
  9. In a monitored scope within the Page_Load method, set the TextBox content to the existing value of the analytics property:
    protected void Page_Load(object sender, EventArgs e)
    {
      using (new SPMonitoredScope("Code6587EN.Ch07.Layouts.Code6587EN.Ch07.ConfigureTrackingCode::Page_Load"))
      {
        if (!IsPostBack)
          if (SPContext.Current.Site.RootWeb.AllProperties.ContainsKey(PropertyName))
            txtScriptBlock.Text = SPContext.Current.Site.RootWeb.AllProperties[PropertyName].ToString();
      }
    }
  10. Add the BtnSubmit_Click method with a monitored scope to execute when the Submit button is clicked:
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
      using (new SPMonitoredScope("Code6587EN.Ch07.Layouts.Code6587EN.Ch07.ConfigureTrackingCode::BtnSubmit_Click"))
      {
      }
    }
  11. If the root site of the site collection already contains the analytics property, set its value as follows:
    if (SPContext.Current.Site.RootWeb.AllProperties.ContainsKey(PropertyName))
      SPContext.Current.Site.RootWeb.AllProperties[PropertyName] = txtScriptBlock.Text;
  12. If the root site of the site collection does not already contain the analytics property, add it and set its value as follows:
    else
      SPContext.Current.Site.RootWeb.AllProperties.Add(PropertyName, txtScriptBlock.Text);
  13. Update the root site of the site collection and redirect to the Site settings page:
    SPContext.Current.Site.RootWeb.Update();
    SPUtility.Redirect(SPContext.Current.Web.ServerRelativeUrl.TrimEnd('/') + "/_layouts/15/Settings.aspx", SPRedirectFlags.Default, HttpContext.Current);
  14. Add an Empty Element item to the project.
  15. In the Elements.xml file of the new element, register our custom action with the URL to our application page:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction Id="AnalyticsCode" GroupId="SiteCollectionAdmin" Location="Microsoft.SharePoint.SiteSettings" Sequence="1000" Title="Configure Analytics Code">
    <UrlAction Url="~sitecollection/_layouts/15/Code6587EN.Ch07/ConfigureTrackingCode.aspx"/>
    </CustomAction>
    </Elements>
  16. Open the feature created previously. Ensure that the new element is included in the feature.
  17. Click on Start from the toolbar to deploy the solution and attach the debugger.
  18. Once the SharePoint site is loaded in the web browser (after clicking on Start), navigate to the Site settings page, and select the Configure Analytics Code link under Site Collection Administration, as shown in the following screenshot:
    How to do it...
  19. Add your analytics tracking code including the <script> tags and submit by clicking on OK:
    How to do it...

How it works...

In this recipe, we have created a basic settings page that allows us to store our analytics tracking code as a property of the root site in the current site collection. This page uses the settings page user controls used by the majority of SharePoint settings pages. In addition, we used a custom action registration to add our settings page to the list of links on the Site settings page in the Site Collection Administration section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.186