Skip to content

Commit

Permalink
✨ [#4115] Add new source ID and secure GUID
Browse files Browse the repository at this point in the history
Added to support having different IDs for when the form is finished or aborted
  • Loading branch information
SilviaAmAm committed Apr 19, 2024
1 parent 80885fe commit 4a39b16
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/openforms/analytics_tools/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ class AnalyticsToolsConfigurationAdmin(TranslationAdmin, SingletonModelAdmin):
_("Analytics: GovMetric"),
{
"fields": (
"govmetric_source_id",
"govmetric_secure_guid",
"govmetric_source_id_form_finished",
"govmetric_source_id_form_aborted",
"govmetric_secure_guid_form_finished",
"govmetric_secure_guid_form_aborted",
"enable_govmetric_analytics",
)
},
Expand Down
6 changes: 4 additions & 2 deletions src/openforms/analytics_tools/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class AnalyticsToolsConfigSerializer(serializers.ModelSerializer):
class Meta:
model = AnalyticsToolsConfiguration
fields = (
"govmetric_source_id",
"govmetric_secure_guid",
"govmetric_source_id_form_finished",
"govmetric_source_id_form_aborted",
"govmetric_secure_guid_form_finished",
"govmetric_secure_guid_form_aborted",
"enable_govmetric_analytics",
)
4 changes: 2 additions & 2 deletions src/openforms/analytics_tools/contrib/govmetric/cookies.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"name": "_pk_id.SOURCE_ID.DOMAIN_HASH",
"name": "_pk_id.SOURCE_ID_FORM_FINISHED.DOMAIN_HASH",
"path": "/"
},
{
"name": "_pk_ses.SOURCE_ID.DOMAIN_HASH",
"name": "_pk_ses.SOURCE_ID_FORM_FINISHED.DOMAIN_HASH",
"path": "/"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Generated by Django 4.2.11 on 2024-04-12 08:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("analytics_tools", "0006_auto_20240112_1046"),
]

operations = [
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_form_aborted",
field=models.CharField(
blank=True,
help_text="Your GovMetric secure GUID for when a form is aborted - This is an optional value. It is created by KLANTINFOCUS when a list of questions is created. It is a string that is unique per set of questions.",
max_length=50,
verbose_name="GovMetric secure GUID form aborted",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_form_finished",
field=models.CharField(
blank=True,
help_text="Your GovMetric secure GUID for when a form is finished - This is an optional value. It is created by KLANTINFOCUS when a list of questions is created. It is a string that is unique per set of questions.",
max_length=50,
verbose_name="GovMetric secure GUID form finished",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_form_aborted",
field=models.CharField(
blank=True,
help_text="Your GovMetric source ID for when a form is aborted - This is created by KLANTINFOCUS when a list of questions is created. It is a numerical value that is unique per set of questions.",
max_length=10,
verbose_name="GovMetric source ID form aborted",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_form_finished",
field=models.CharField(
blank=True,
help_text="Your GovMetric source ID for when a form is finished - This is created by KLANTINFOCUS when a list of questions is created. It is a numerical value that is unique per set of questions.",
max_length=10,
verbose_name="GovMetric source ID form finished",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_form_aborted_en",
field=models.CharField(
blank=True,
help_text="Your GovMetric secure GUID for when a form is aborted - This is an optional value. It is created by KLANTINFOCUS when a list of questions is created. It is a string that is unique per set of questions.",
max_length=50,
null=True,
verbose_name="GovMetric secure GUID form aborted",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_form_aborted_nl",
field=models.CharField(
blank=True,
help_text="Your GovMetric secure GUID for when a form is aborted - This is an optional value. It is created by KLANTINFOCUS when a list of questions is created. It is a string that is unique per set of questions.",
max_length=50,
null=True,
verbose_name="GovMetric secure GUID form aborted",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_form_finished_en",
field=models.CharField(
blank=True,
help_text="Your GovMetric secure GUID for when a form is finished - This is an optional value. It is created by KLANTINFOCUS when a list of questions is created. It is a string that is unique per set of questions.",
max_length=50,
null=True,
verbose_name="GovMetric secure GUID form finished",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_form_finished_nl",
field=models.CharField(
blank=True,
help_text="Your GovMetric secure GUID for when a form is finished - This is an optional value. It is created by KLANTINFOCUS when a list of questions is created. It is a string that is unique per set of questions.",
max_length=50,
null=True,
verbose_name="GovMetric secure GUID form finished",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_form_aborted_en",
field=models.CharField(
blank=True,
help_text="Your GovMetric source ID for when a form is aborted - This is created by KLANTINFOCUS when a list of questions is created. It is a numerical value that is unique per set of questions.",
max_length=10,
null=True,
verbose_name="GovMetric source ID form aborted",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_form_aborted_nl",
field=models.CharField(
blank=True,
help_text="Your GovMetric source ID for when a form is aborted - This is created by KLANTINFOCUS when a list of questions is created. It is a numerical value that is unique per set of questions.",
max_length=10,
null=True,
verbose_name="GovMetric source ID form aborted",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_form_finished_en",
field=models.CharField(
blank=True,
help_text="Your GovMetric source ID for when a form is finished - This is created by KLANTINFOCUS when a list of questions is created. It is a numerical value that is unique per set of questions.",
max_length=10,
null=True,
verbose_name="GovMetric source ID form finished",
),
),
migrations.AddField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_form_finished_nl",
field=models.CharField(
blank=True,
help_text="Your GovMetric source ID for when a form is finished - This is created by KLANTINFOCUS when a list of questions is created. It is a numerical value that is unique per set of questions.",
max_length=10,
null=True,
verbose_name="GovMetric source ID form finished",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated by Django 4.2.11 on 2024-04-12 08:43

from django.db import migrations

from openforms.translations.utils import get_language_codes


def add_extra_source_id_govmetric(apps, schema_editor):
AnalyticsToolsConfiguration = apps.get_model(
"analytics_tools", "AnalyticsToolsConfiguration"
)

config = AnalyticsToolsConfiguration.objects.all().first()
if not config:
return

language_codes = get_language_codes()

for language_code in language_codes:
if old_id := getattr(config, f"govmetric_source_id_{language_code}"):
setattr(config, f"govmetric_source_id_form_aborted_{language_code}", old_id)
setattr(
config, f"govmetric_source_id_form_finished_{language_code}", old_id
)

if old_guid := getattr(config, f"govmetric_secure_guid_{language_code}"):
setattr(
config, f"govmetric_secure_guid_form_aborted_{language_code}", old_guid
)
setattr(
config, f"govmetric_secure_guid_form_finished_{language_code}", old_guid
)

config.save()


def remove_extra_source_id_govmetric(apps, schema_editor):
AnalyticsToolsConfiguration = apps.get_model(
"analytics_tools", "AnalyticsToolsConfiguration"
)

config = AnalyticsToolsConfiguration.objects.first()
if not config:
return

language_codes = get_language_codes()

for language_code in language_codes:
new_id = getattr(
config, f"govmetric_source_id_form_finished_{language_code}"
) or getattr(config, f"govmetric_source_id_form_aborted_{language_code}")
if new_id:
setattr(config, f"govmetric_source_id_{language_code}", new_id)

new_guid = getattr(
config, f"govmetric_secure_guid_form_finished_{language_code}"
) or getattr(config, f"govmetric_secure_guid_form_aborted_{language_code}")
if new_guid:
setattr(config, f"govmetric_secure_guid_{language_code}", new_guid)

config.save()


class Migration(migrations.Migration):
dependencies = [
(
"analytics_tools",
"0007_analyticstoolsconfiguration_govmetric_secure_guid_form_abort_and_more",
),
]

operations = [
migrations.RunPython(
add_extra_source_id_govmetric, remove_extra_source_id_govmetric
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 4.2.11 on 2024-04-12 09:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("analytics_tools", "0008_convert_ids_govmetric"),
]

operations = [
migrations.RemoveField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid",
),
migrations.RemoveField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_en",
),
migrations.RemoveField(
model_name="analyticstoolsconfiguration",
name="govmetric_secure_guid_nl",
),
migrations.RemoveField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id",
),
migrations.RemoveField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_en",
),
migrations.RemoveField(
model_name="analyticstoolsconfiguration",
name="govmetric_source_id_nl",
),
]
47 changes: 37 additions & 10 deletions src/openforms/analytics_tools/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ def has_enabled_toggled(
enable_field_name="enable_govmetric_analytics",
is_enabled_property="is_govmetric_enabled",
replacements=[
StringReplacement(needle="SOURCE_ID", field_name="govmetric_source_id"),
StringReplacement(
needle="SOURCE_ID_FORM_FINISHED",
field_name="govmetric_source_id_form_finished",
),
StringReplacement(needle="DOMAIN_HASH", callback=get_domain_hash),
],
),
Expand Down Expand Up @@ -204,22 +207,42 @@ class AnalyticsToolsConfiguration(SingletonModel):
default=False,
help_text=_("Enabling this installs SiteImprove"),
)
govmetric_source_id = models.CharField(
_("GovMetric source ID"),
govmetric_source_id_form_aborted = models.CharField(
_("GovMetric source ID form aborted"),
max_length=10,
blank=True,
help_text=_(
"Your GovMetric source ID - This is created by KLANTINFOCUS when a list of questions is created. "
"It is a numerical value that is unique per set of questions."
"Your GovMetric source ID for when a form is aborted - This is created by KLANTINFOCUS when a list of "
"questions is created. It is a numerical value that is unique per set of questions."
),
)
govmetric_secure_guid = models.CharField(
_("GovMetric secure GUID"),
govmetric_secure_guid_form_aborted = models.CharField(
_("GovMetric secure GUID form aborted"),
blank=True,
max_length=50,
help_text=_(
"Your GovMetric secure GUID - This is an optional value. It is created by KLANTINFOCUS when a list "
"of questions is created. It is a string that is unique per set of questions."
"Your GovMetric secure GUID for when a form is aborted - This is an optional value. "
"It is created by KLANTINFOCUS when a list of questions is created. "
"It is a string that is unique per set of questions."
),
)
govmetric_source_id_form_finished = models.CharField(
_("GovMetric source ID form finished"),
max_length=10,
blank=True,
help_text=_(
"Your GovMetric source ID for when a form is finished - This is created by KLANTINFOCUS when a list of "
"questions is created. It is a numerical value that is unique per set of questions."
),
)
govmetric_secure_guid_form_finished = models.CharField(
_("GovMetric secure GUID form finished"),
blank=True,
max_length=50,
help_text=_(
"Your GovMetric secure GUID for when a form is finished - This is an optional value. "
"It is created by KLANTINFOCUS when a list of questions is created. "
"It is a string that is unique per set of questions."
),
)
enable_govmetric_analytics = models.BooleanField(
Expand Down Expand Up @@ -300,7 +323,11 @@ def is_google_analytics_enabled(self) -> bool:

@property
def is_govmetric_enabled(self) -> bool:
return self.govmetric_source_id and self.enable_govmetric_analytics
required_fields_present = (
self.govmetric_source_id_form_finished
and self.govmetric_source_id_form_aborted
)
return required_fields_present and self.enable_govmetric_analytics

def save(self, *args, **kwargs):
# If instance is being created, we can't find original values
Expand Down
14 changes: 12 additions & 2 deletions src/openforms/analytics_tools/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@

@register(AnalyticsToolsConfiguration)
class AnalyticsToolsConfigurationTranslationOptions(TranslationOptions):
fields = ("govmetric_source_id", "govmetric_secure_guid")
fallback_undefined = {"govmetric_source_id": "", "govmetric_secure_guid": ""}
fields = (
"govmetric_source_id_form_aborted",
"govmetric_secure_guid_form_aborted",
"govmetric_source_id_form_finished",
"govmetric_secure_guid_form_finished",
)
fallback_undefined = {
"govmetric_source_id_form_aborted": "",
"govmetric_secure_guid_form_aborted": "",
"govmetric_source_id_form_finished": "",
"govmetric_secure_guid_form_finished": "",
}

0 comments on commit 4a39b16

Please sign in to comment.