Sitecore integration

This article will help you get started with Cookie Information and the Sitecore integration.

Katarina Hansen avatar
Written by Katarina Hansen
Updated over a week ago

Prerequisites for using the Cookie Information Sitecore extension

In order to be able to use the Sitecore extension, you need a few things:

  • An account with Cookie Information. If you haven't created one yet, please visit our webiste and sign up.

  • Your domain must be added to the Cookie Information Platform.

  • A Visual Studio project with Sitecore set up and running. If you have yet to do this, please refer to the Sitecore documentation for setting up a project.

Installing the Cookie Information Sitecore extension

The Cookie Information Sitecore extension can be found as a NuGet package. You can either download the package or install it via the command line using the following command:

Install-Package CookieInformation.SitecoreExtensions -Version 1.0.4

Alternatively, there are also commands available for other package managers:

.NET CLI:

dotnet add package CookieInformation.SitecoreExtensions --version 1.0.4

Package Reference:

<PackageReference Include="CookieInformation.SitecoreExtensions" Version="1.0.4" />

Paket CLI:

paket add CookieInformation.SitecoreExtensions --version 1.0.4

Activating necessary configuration files

Once you have installed the package with your chosen command line tool, you will need to rename a couple of configuration files in order for them to become active and useable.

  • Rename AppSettings.config.disabled to AppSettings.config

  • Rename Pipeline.config.disabled to Pipeline.config

Configuring the Cookie Information Sitecore extension

By default, the possible cookie categories available to use are:

  • NECESSARY

  • FUNCTIONAL

  • MARKETING

  • STATISTIC

To assign cookies to each category, go to the following directory in your project:

App_Config/Include/CookieInformation.SitecoreExtensions/AppSettings.config

The first thing you will see is the mappings of cookie categories that looks like the following:

CookieInformation.SitecoreExtensions:NECESSARY
CookieInformation.SitecoreExtensions:FUNCTIONAL
CookieInformation.SitecoreExtensions:MARKETING
CookieInformation.SitecoreExtensions:STATISTIC
CookieInformation.SitecoreExtensions:MISC

You will also see that the most commonly used cookies have been added already to make it easier for you to get up and running. Please note that each key should have a list of cookie names separated by a comma (",").

Additional Settings

There are also some separate additional settings that you can adjust to tailor the integration's behaviour to your own liking:

CookieInformation.SitecoreExtensions:REMOVEOLD

REMOVEOLD will default to false. If it is set to true then cookies in a visitors browser that they have not given consent to will expire. At the same time, it also triggers the Pipeline (found in App_Config/Include/CookieInformation.SitecoreExtensions/Pipeline.config).

CookieInformation.SitecoreExtensions:LOGREMOVAL

LOGREMOVAL will also default to false. If it is set to true then it will log in INFO whenever a cookie is removed due to missing consent.

CookieInformation.SitecoreExtensions:COOKIENAME

COOKIENAME will default to CookieInformationConsent, but otherwise will show the name of the cookie that is set.

CookieInformation.SitecoreExtensions:DISABLECOOKIECONSENTFORAUTHENTICATEDUSERS

DISABLECOOKIECONSENTFORAUTHETICATEDUSERS defaults to true and disables cookie consent functionality for users that are authenticated.

Useful References

@using CookieInformation.SitecoreExtensions.Services
@using CookieInformation.SitecoreExtensions.Settings
@using CookieInformation.SitecoreExtensions.Helpers

Adding Cookie Information JavaScript to the <head> tag of your site

Similarly to how you would implement the scripts for Cookie Information on other platforms, you can simply navigate to the layout of your site and add the below line in the <head> tag:

@Html.Raw(CookieServiceLocator.CookieService.GetConsentScript())

There are a few different ways you can add a cookie. Below are some examples.

@if (CookieServiceLocator.CookieService.ConsentsToCategory(CookieInformationCategories.MARKETING))
{
HttpContext.Current.Response.Cookies.Add(new System.Web.HttpCookie("MyInternalTracking", "TrackingID"));
@Html.Sitecore().VisitorIdentification()
}

You can also add scripts as usual, if you prefer:

@if (CookieServiceLocator.CookieService.ConsentsToCategory(CookieInformationCategories.FUNCTIONAL))
{
<script src="//maps.googleapis.com/maps/api/js?key=@(Sitecore.Configuration.Settings.GetSetting("Google.API"))&amp;callback=initMap" async defer></script>
}

Allowing script to fire immediately upon user acceptance of cookies

If you would like a script to fire immediately on a page as soon as the visitor has accepted a particular cookie category (such as Google), then you can use the following code snippet:

<!-- Google Analytics -->
@using (Html.CreateConditionalCookieScript(CookieInformationCategories.STATISTIC, "initGA()"))
{
<script>
function initGA() {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
}
<!-- End Google Analytics -->

In order for it to work correctly, please remember to change the attribute 'UA-XXXXX-Y' to your own Google Analytics ID.

Implementation checklist

  1. A Cookie Information account with a domain added to the platform

  2. An up and running Sitecore project

  3. Extension installed via NuGet or your package manager of choice

  4. Renamed configuration files

  5. Cookies assigned

  6. JavaScript added to <head> tag

Related articles:


โ€‹

Did this answer your question?