From 9317a3e915c2337d82934f43c1f2ef49bd3f6fc7 Mon Sep 17 00:00:00 2001 From: Christos Melas Date: Thu, 2 Mar 2023 14:02:20 +0200 Subject: [PATCH] Handle sbm_tag param --- spec/actions_manager_spec.coffee | 115 ++++++++++++++++++++++++++++--- src/actions_manager.coffee | 23 +++---- 2 files changed, 118 insertions(+), 20 deletions(-) diff --git a/spec/actions_manager_spec.coffee b/spec/actions_manager_spec.coffee index 8ab2be9..0cbc5c4 100644 --- a/spec/actions_manager_spec.coffee +++ b/spec/actions_manager_spec.coffee @@ -392,13 +392,13 @@ describe 'ActionsManager', -> it 'does not execute redirect callback', -> expect(@redirect_callback_spy).to.not.be.called - context 'when the sbm param is not set', -> + context 'when the sbm params are not set', -> before -> @data = JSON.stringify({}) @run() @payload = @sendbeacon_spy.args[0][1] - it 'should not include the sbm tag in the metadata tags', -> + it 'should not include sbm tags in the metadata tags', -> expect(@payload.metadata.tags).to.equal '' context 'when the sbm param is set', -> @@ -410,15 +410,33 @@ describe 'ActionsManager', -> it 'should include the sbm tag in the metadata tags', -> expect(@payload.metadata.tags).to.equal 'sbm' + context 'when an sbm_tag param is set', -> + before -> + @data = JSON.stringify({ sbm_tag: 'foo' }) + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the respective sbm tag in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'foo' + + context 'when both sbm and sbm_tag params are set', -> + before -> + @data = JSON.stringify({ sbm: true, sbm_tag: 'foo' }) + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tags in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'sbm,foo' + context 'when additional tags exist in the metadata tags', -> - context 'when the sbm param is not set', -> + context 'when the sbm params are not set', -> before -> @data = JSON.stringify({}) @session.metadata = { app_type: 'web', cp: 'f', tags: 'not_sbm' } @run() @payload = @sendbeacon_spy.args[0][1] - it 'should include the sbm tag in the metadata tags', -> + it 'should not include sbm tags in the metadata tags', -> expect(@payload.metadata.tags).to.equal 'not_sbm' context 'when the sbm param is set', -> @@ -431,15 +449,35 @@ describe 'ActionsManager', -> it 'should include the sbm tag in the metadata tags', -> expect(@payload.metadata.tags).to.equal 'not_sbm,sbm' - context 'when an sbm tag already exists in metadata tags', -> - context 'when the sbm param is not set', -> + context 'when an sbm_tag param is set', -> + before -> + @data = JSON.stringify({ sbm_tag: 'foo' }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'not_sbm' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the respective sbm tag in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'not_sbm,foo' + + context 'when both sbm and sbm_tag params are set', -> + before -> + @data = JSON.stringify({ sbm: true, sbm_tag: 'foo' }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'not_sbm' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tags in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'not_sbm,sbm,foo' + + context 'when the sbm tag already exists in metadata tags', -> + context 'when the sbm params are not set', -> before -> @data = JSON.stringify({}) @session.metadata = { app_type: 'web', cp: 'f', tags: 'sbm' } @run() @payload = @sendbeacon_spy.args[0][1] - it 'should include the sbm tag in the metadata tags', -> + it 'should not include sbm tags in the metadata tags', -> expect(@payload.metadata.tags).to.equal 'sbm' context 'when the sbm param is set', -> @@ -449,9 +487,70 @@ describe 'ActionsManager', -> @run() @payload = @sendbeacon_spy.args[0][1] - it 'should include the sbm tag in the metadata tags', -> + it 'should include the sbm tag in the metadata tags only once', -> expect(@payload.metadata.tags).to.equal 'sbm' + context 'when an sbm_tag param is set', -> + before -> + @data = JSON.stringify({ sbm_tag: 'foo' }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'sbm' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tags in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'sbm,foo' + + context 'when both sbm and sbm_tag params are set', -> + before -> + @data = JSON.stringify({ sbm: true, sbm_tag: 'foo' }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'sbm' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tags in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'sbm,foo' + + context 'when an sbm_tag already exists in metadata tags', -> + context 'when the sbm params are not set', -> + before -> + @data = JSON.stringify({}) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'foo' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should not include sbm tags in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'foo' + + context 'when the sbm param is set', -> + before -> + @data = JSON.stringify({ sbm: true }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'foo' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tag in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'foo,sbm' + + context 'when an sbm_tag param is set', -> + before -> + @data = JSON.stringify({ sbm_tag: 'foo' }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'foo' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tag in the metadata tags only once', -> + expect(@payload.metadata.tags).to.equal 'foo' + + context 'when both sbm and sbm_tag params are set', -> + before -> + @data = JSON.stringify({ sbm: true, sbm_tag: 'foo' }) + @session.metadata = { app_type: 'web', cp: 'f', tags: 'foo' } + @run() + @payload = @sendbeacon_spy.args[0][1] + + it 'should include the sbm tag in the metadata tags', -> + expect(@payload.metadata.tags).to.equal 'foo,sbm' + describe 'ecommerce', -> beforeEach -> @category = 'ecommerce' diff --git a/src/actions_manager.coffee b/src/actions_manager.coffee index 75c090b..4ebd06a 100644 --- a/src/actions_manager.coffee +++ b/src/actions_manager.coffee @@ -79,7 +79,7 @@ define [ payload[params.url] = Settings.url.current payload[params.referrer] = Settings.url.referrer payload[params.shop_code] = @session.shop_code - payload[params.metadata] = @_buildMetadata(JSON.parse(data).sbm) + payload[params.metadata] = @_buildMetadata(JSON.parse(data)) payload[params.cookie_policy] = @session.cookie_policy payload[params.actions] = [{ category: category @@ -89,27 +89,26 @@ define [ payload - _buildMetadata: (sbm) -> + _buildMetadata: (data) -> # When using 3rd-party cookies, metadata will be undefined # In this case, the server will fallback to using the meta cookie return '' unless @session.metadata + sbm = data.sbm + sbm_tag = data.sbm_tag + # Return metadata as is if sbm is not present - return @session.metadata unless sbm + return @session.metadata unless sbm || sbm_tag # Clone meta = JSON.parse(JSON.stringify(@session.metadata)) - # If no tags are present, return a single sbm tag - unless meta.tags - meta.tags = 'sbm' - return meta - - # Return metadata as is if sbm is already present - return meta if 'sbm' in meta.tags.split(',') + tags = if meta.tags then meta.tags.split(',') else [] + if sbm || sbm_tag + tags.push('sbm') if sbm && !('sbm' in tags) + tags.push(sbm_tag) if sbm_tag && !(sbm_tag in tags) - # Otherwise, append the sbm tag - meta.tags = meta.tags + ',sbm' + meta.tags = tags.join() if tags meta