Getting Started
Since version 2.19
Get started by learning how to track analytics events across your entire application.
Front-Commerce uses analytics
under the hood. If
we represent how it works, it would look like this:
Across your React application, you can track events using functions such as
trackEvent
or trackPage
. Then, the event is dispatched to all the relevant
plugins registered in your application, learn more in the
Tracking API docs.
This means that once you have correctly configured events in your React Components, adding new tracking services is less risky: it has no impact on what is being tracked.
Configure analytics
If you would like to directly jump into the code, you look at the common plugins often used within a Front-Commerce app.
First you will need to configure your analytics by updating the configuration file:
module.exports = {
analytics: {
// Make sure that your analytics is enabled
enable: true,
// Enables the debug mode of the `analytics` library
debug: true,
// Pass any default settings to your plugins
defaultSettings: {},
// The list of plugins is defined here
plugins: [
{
// The name allows to know if the user allowed to use
// this tracking service or not, it should not be confused
// with the name in the plugin script.
name: "google-analytics",
// Usually we always need to set it to true since GDPR
needConsent: true,
// Some plugins may have a privacy mode allowing them
// to be enabled by default. Use this configuration to always
// enable it. It is irrelevant if `needConsent` is `false`.
enabledByDefault: true,
// Settings can be an object or a function that will be called
// with the current consent authorization from the visitor, it also
// receive a 2nd parameter with codes of the other consents given.
// Using a function can allow to have different settings depending
// on the context
settings: (authorization, otherAuthorizations) => {
return {
measurementIds: ["G-abc123"],
gtagConfig: {
anonymize_ip: !authorization,
},
};
},
// It should either return a promise function or a dynamic import to
// the plugin script which will be added at to the `analytics` library
// see listed plugins: https://getanalytics.io/plugins/#supported-analytic-tools
script: () => import("@analytics/google-analytics"),
},
],
},
};
Retrieve authorized cookie services in analytics
In Front-Commerce, to allow authorized cookie services to be injected in
analytics services settings(authorization, otherAuthorizations)
callback (as
its 2nd parameter), those services also need to be defined in
config/analytics.js
.
This can be done by declaring "dummy" modules in config/analytics.js
such as:
// ...
+ {
+ name: "my-service",
+ needConsent: true,
+ script: () => () => {
+ return {
+ name: "my-service",
+ };
+ },
+ },
],
},
};
The GDPR consent
If your plugins need consent of the user before running, you need to setup the
cookiesServices.js
file. This file will let you define which cookies and
trackings services are used within your application and will let the user chose
which tracking service to allow.
export default {
default_en: [
{
// Category of cookies to allow the user to accept all the plugins at once in a specific category
title: "Analytics",
description:
"These cookies allows us to measure the traffic on our contents and hence to improve them.",
services: [
{
// The name should be the same as mentioned in the `config/analytics.js` file
name: "google-analytics",
title: "Google Analytics",
// display all the cookies managed by Google Analytics
cookies: [
"_ga",
"_gat",
"_gid",
"__utma",
"__utmb",
"__utmc",
"__utmt",
"__utmz",
],
description:
"Google Analytics cookies, from Google, are meant to gather statistics about visits.",
link: "https://support.google.com/analytics/answer/6004245",
},
],
},
],
};
The consent for the cookies is stored in 2 cookies:
hasConsent
cookie which stores if the user provided consent answer (authorized or denied) for all services.authorizations
cookie which stores a JSON string of all consents given in the following format{ [service_name]: true | false, ... }
The expiration for these two cookies' should be configured in
src/config/website.js
.
module.exports = {
default_image_url: "https://placehold.it/150x220",
available_page_sizes: [18, 36],
....
rewrittenToRoot: ["/home"],
useUserTitles: false,
cookieMaxAgeInMonths: 12,
};