From 3b4e5e0afa1ad9c8f35ba9ec81c39aebf7b04b3f Mon Sep 17 00:00:00 2001 From: MrGoumX Date: Thu, 11 Jan 2024 12:59:28 +0200 Subject: [PATCH] Add ActionsManager#_determineCookieType --- config/settings/default.yml | 1 + spec/actions_manager_spec.coffee | 32 +++++++++++++++++++++++++++++++- src/actions_manager.coffee | 7 ++++++- src/settings.coffee.sample | 1 + 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/config/settings/default.yml b/config/settings/default.yml index 576a6a7..44ff479 100644 --- a/config/settings/default.yml +++ b/config/settings/default.yml @@ -60,3 +60,4 @@ params: metadata: 'metadata' actions: 'actions' cookie_policy: 'cp' + cookie_type: 'ct' diff --git a/spec/actions_manager_spec.coffee b/spec/actions_manager_spec.coffee index 0cbc5c4..eecdbcb 100644 --- a/spec/actions_manager_spec.coffee +++ b/spec/actions_manager_spec.coffee @@ -26,6 +26,10 @@ VALID_LINE_ITEM_DATA_2 = { } VALID_LINE_ITEM_DATA_2_STRINGIFIED = JSON.stringify(VALID_LINE_ITEM_DATA_2) +clear_all_cookies = -> + document.cookie.split(/;\s/g).forEach (cookie)-> + document.cookie = "#{cookie.split('=')[0]}= ;expires=Thu, 01 Jan 1970 00:00:01 GMT" + api_session_promise_tests = -> context 'when session retrieval fulfils', -> beforeEach -> @@ -68,7 +72,7 @@ action_reporting_tests = (reported_position) -> p = @settings.params expect(@payload).to.have.keys p.url, p.referrer, p.shop_code, p.cookie_policy, p.metadata, - p.actions + p.actions, p.cookie_type it 'has proper data in url key', -> expect(@payload.url).to.equal @settings.url.current @@ -106,6 +110,32 @@ action_reporting_tests = (reported_position) -> type: @type data: JSON.stringify(data) + describe 'cookie_type', -> + context 'when 1st party cookie exists', -> + beforeEach -> + # Clear all cookies + clear_all_cookies() + + # Set basic 1st party cookie + document.cookie = "#{@settings.cookies.basic.session.name}=bar;" + + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'has proper data in cookie_type key', -> + expect(@payload.ct).to.equal '1' + + context 'when 1st party cookie does not exist', -> + beforeEach -> + # Clear all cookies + clear_all_cookies() + + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'has proper data in cookie_type key', -> + expect(@payload.ct).to.equal '3' + describe 'ActionsManager', -> before (done) -> @analytics_session = 'analytics_session' diff --git a/src/actions_manager.coffee b/src/actions_manager.coffee index 4ebd06a..adadb60 100644 --- a/src/actions_manager.coffee +++ b/src/actions_manager.coffee @@ -2,9 +2,10 @@ define [ 'settings' 'reporter' 'runnable' + 'biskoto' 'validator' 'analytics_url' -], (Settings, Reporter, Runnable, Validator, AnalyticsUrl) -> +], (Settings, Reporter, Runnable, Biskoto, Validator, AnalyticsUrl) -> class ActionsManager ActionsManager::[key] = method for key, method of Runnable @@ -81,6 +82,7 @@ define [ payload[params.shop_code] = @session.shop_code payload[params.metadata] = @_buildMetadata(JSON.parse(data)) payload[params.cookie_policy] = @session.cookie_policy + payload[params.cookie_type] = @_determineCookieType() payload[params.actions] = [{ category: category type: type @@ -112,4 +114,7 @@ define [ meta + _determineCookieType: -> + if Biskoto.get(Settings.cookies.basic.session.name) then '1' else '3' + return ActionsManager diff --git a/src/settings.coffee.sample b/src/settings.coffee.sample index 7ceff56..893cc26 100644 --- a/src/settings.coffee.sample +++ b/src/settings.coffee.sample @@ -38,6 +38,7 @@ define -> metadata: '@@params.metadata' actions: '@@params.actions' cookie_policy: '@@params.cookie_policy' + cookie_type: '@@params.cookie_type' ### Cookies