Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/handle-sbm-tag'
Browse files Browse the repository at this point in the history
Merges #96
  • Loading branch information
mrwhizzy committed Mar 2, 2023
2 parents d0e9eb7 + 9317a3e commit 3df7270
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 20 deletions.
115 changes: 107 additions & 8 deletions spec/actions_manager_spec.coffee
Expand Up @@ -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', ->
Expand All @@ -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', ->
Expand All @@ -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', ->
Expand All @@ -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'
Expand Down
23 changes: 11 additions & 12 deletions src/actions_manager.coffee
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 3df7270

Please sign in to comment.