Constrain PostHog consent to one normal category per banner
Add a partial unique index ensuring only one category per banner can have posthog_consent enabled. Default it to the analytics category on banner creation, clear the previous mapping before setting a new one, and restrict the toggle to NORMAL categories in both the service layer and the console UI. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -25,12 +25,53 @@ var defaultCategories = []struct {
|
||||
Kind coredata.CookieCategoryKind
|
||||
Rank int
|
||||
GCMConsentTypes []string
|
||||
PostHogConsent bool
|
||||
}{
|
||||
{"Necessary", "necessary", "Essential cookies required for the website to function properly.", coredata.CookieCategoryKindNecessary, 0, []string{"security_storage"}},
|
||||
{"Analytics", "analytics", "Cookies that help understand how visitors interact with the website.", coredata.CookieCategoryKindNormal, 1, []string{"analytics_storage"}},
|
||||
{"Advertising", "advertising", "Cookies used to deliver relevant advertisements and track campaigns.", coredata.CookieCategoryKindNormal, 2, []string{"ad_storage", "ad_user_data", "ad_personalization"}},
|
||||
{"Functional", "functional", "Cookies that enable enhanced functionality and personalization.", coredata.CookieCategoryKindNormal, 3, []string{"functionality_storage", "personalization_storage"}},
|
||||
{"Uncategorised", "uncategorised", "Cookies that have not been assigned to a category yet.", coredata.CookieCategoryKindUncategorised, 4, nil},
|
||||
{
|
||||
Name: "Necessary",
|
||||
Slug: "necessary",
|
||||
Description: "Essential cookies required for the website to function properly.",
|
||||
Kind: coredata.CookieCategoryKindNecessary,
|
||||
Rank: 0,
|
||||
GCMConsentTypes: []string{"security_storage"},
|
||||
PostHogConsent: false,
|
||||
},
|
||||
{
|
||||
Name: "Analytics",
|
||||
Slug: "analytics",
|
||||
Description: "Cookies that help understand how visitors interact with the website.",
|
||||
Kind: coredata.CookieCategoryKindNormal,
|
||||
Rank: 1,
|
||||
GCMConsentTypes: []string{"analytics_storage"},
|
||||
PostHogConsent: true,
|
||||
},
|
||||
{
|
||||
Name: "Advertising",
|
||||
Slug: "advertising",
|
||||
Description: "Cookies used to deliver relevant advertisements and track campaigns.",
|
||||
Kind: coredata.CookieCategoryKindNormal,
|
||||
Rank: 2,
|
||||
GCMConsentTypes: []string{"ad_storage", "ad_user_data", "ad_personalization"},
|
||||
PostHogConsent: false,
|
||||
},
|
||||
{
|
||||
Name: "Functional",
|
||||
Slug: "functional",
|
||||
Description: "Cookies that enable enhanced functionality and personalization.",
|
||||
Kind: coredata.CookieCategoryKindNormal,
|
||||
Rank: 3,
|
||||
GCMConsentTypes: []string{"functionality_storage", "personalization_storage"},
|
||||
PostHogConsent: false,
|
||||
},
|
||||
{
|
||||
Name: "Uncategorised",
|
||||
Slug: "uncategorised",
|
||||
Description: "Cookies that have not been assigned to a category yet.",
|
||||
Kind: coredata.CookieCategoryKindUncategorised,
|
||||
Rank: 4,
|
||||
GCMConsentTypes: nil,
|
||||
PostHogConsent: false,
|
||||
},
|
||||
}
|
||||
|
||||
var defaultCategoryTranslationsByLanguage = map[string]map[string]struct {
|
||||
|
||||
@@ -33,4 +33,5 @@ var (
|
||||
ErrCookieNameAlreadyExists = errors.New("a cookie with this name already exists in this banner")
|
||||
ErrCategoriesBannerMismatch = errors.New("source and target categories belong to different banners")
|
||||
ErrSameCategoryMove = errors.New("source and target cookie categories must be different")
|
||||
ErrPostHogConsentKindInvalid = errors.New("PostHog consent can only be enabled on normal categories")
|
||||
)
|
||||
|
||||
@@ -574,6 +574,7 @@ func (s *Service) CreateCookieBanner(
|
||||
Kind: dc.Kind,
|
||||
Rank: dc.Rank,
|
||||
GCMConsentTypes: gcmConsentTypes,
|
||||
PostHogConsent: dc.PostHogConsent,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
@@ -1350,6 +1351,15 @@ func (s *Service) UpdateCookieCategory(
|
||||
category.GCMConsentTypes = *req.GCMConsentTypes
|
||||
}
|
||||
if req.PostHogConsent != nil {
|
||||
if *req.PostHogConsent && category.Kind != coredata.CookieCategoryKindNormal {
|
||||
return ErrPostHogConsentKindInvalid
|
||||
}
|
||||
if *req.PostHogConsent {
|
||||
var categories coredata.CookieCategories
|
||||
if err := categories.ClearPostHogConsentByBannerID(ctx, tx, scope, category.CookieBannerID); err != nil {
|
||||
return fmt.Errorf("cannot clear posthog consent: %w", err)
|
||||
}
|
||||
}
|
||||
category.PostHogConsent = *req.PostHogConsent
|
||||
}
|
||||
|
||||
@@ -2091,13 +2101,13 @@ func (s *Service) ReportDetectedCookies(
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if err := cookie.Insert(ctx, tx, scope); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceAlreadyExists) {
|
||||
continue
|
||||
}
|
||||
ok, err := cookie.InsertIfNotExists(ctx, tx, scope)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert detected cookie: %w", err)
|
||||
}
|
||||
inserted++
|
||||
if ok {
|
||||
inserted++
|
||||
}
|
||||
}
|
||||
|
||||
if inserted > 0 {
|
||||
|
||||
Reference in New Issue
Block a user