Skip to content

Add support for named global context - #1331

Merged
Jethro Nederhof (jethron) merged 1 commit into
snowplow:release/4.0.0from
jethron:PE-5311-namedctx
Jul 23, 2024
Merged

Add support for named global context#1331
Jethro Nederhof (jethron) merged 1 commit into
snowplow:release/4.0.0from
jethron:PE-5311-namedctx

Conversation

@jethron

Copy link
Copy Markdown
Contributor

This is a proposal for "named global context"; an update to the addGlobalContexts/removeGlobalContexts APIs to allow naming each primitive/generator.

Once named, you can remove them by referencing just the name, but you can also upsert the value by adding via the same name again. This allows avoiding gymnastics required by the current API, where to update a primitive you have to have the original value around in order to remove it in order to add it again without creating duplicates.

@bundlemon

bundlemon Bot commented Jul 17, 2024

Copy link
Copy Markdown

BundleMon

Files added (6)
Status Path Size Limits
libraries/browser-tracker-core/dist/index.mod
ule.js
+27.33KB 28KB / +10%
trackers/javascript-tracker/dist/sp.js
+26.24KB 25.5KB / +10%
trackers/javascript-tracker/dist/sp.lite.js
+15.56KB 16KB / +10%
trackers/browser-tracker/dist/index.umd.min.j
s
+15.42KB 16KB / +10%
libraries/tracker-core/dist/index.module.js
+13.64KB 15KB / +10%
trackers/browser-tracker/dist/index.module.js
+3.49KB 5KB / +10%

Total files change +101.68KB 0%

Final result: ❌

View report in BundleMon website ➡️


Current branch size history

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the approach and that it is done in a non-breaking way!

I am a bit unsure whether we should keep providing so many ways to configure global context or rather should deprecate the non-named approach and just guide users to name their global context. It makes it more confusing for users if the API supports so many variations. On the other hand, global context is a commonly used feature and making the change non-breaking should speed up the adoption of the v4, so this solution is very tempting.

I must be doing something stupid, but for some reason I don't see the global context entities configured using the named approach in events tracked from a demo React app. I do see the ones configured with the unnamed approach though. This is the way I am adding the context:

  // this one doesn't show up in events
  addGlobalContexts({
    c1: {
      schema: "iglu:com.acme_company/page/jsonschema/1-0-0",
      data: {
        title: "Page 1",
      },
    },
  });

  // this one gets tracked in events
  addGlobalContexts([
    {
      schema: "iglu:com.acme_company/page/jsonschema/1-0-0",
      data: {
        title: "Page 2",
      },
    },
  ]);

Do you see anything obvious that I am doing wrong here? I'll give it another shot tomorrow.

@jethron

Copy link
Copy Markdown
Contributor Author

Nope, that looks fine to me. I can't replicate this issue, it works as expected for both JS tracker and Browser tracker when I build it. 😕 Did you do a full rebuild to get the changes in tracker-core etc?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was my confusion, for some reason I thought that global context was in the browser-tracker-core, not in the tracker-core.

It all works very nicely and is a solid improvement! I am still a bit unsure about having multiple options to do the same thing in the API, but I think we can make the trade-off towards making it easier to upgrade to v4 for users and do a better job in the docs to make sure that people use the named approach.

@jethron
Jethro Nederhof (jethron) merged commit b162da0 into snowplow:release/4.0.0 Jul 23, 2024
@jethron
Jethro Nederhof (jethron) deleted the PE-5311-namedctx branch July 23, 2024 02:08
Matus Tomlein (matus-tomlein) pushed a commit that referenced this pull request Jul 26, 2024
The `addGlobalContexts` API now accepts an `object` mapping "names" to context primitives or context generators, rather than just an array of "unnamed" global entities/generators.

Calling `addGlobalContexts` multiple times with values of the same "name" will overwrite the previous value for subsequent events, allowing "upserting" or updating of global context without having to remove the previous version first and introducing duplicates.

`removeGlobalContexts` can now also accept plain strings which are handled as names, so primitives/generators can be removed by name without having to own a reference to the generating function or an object that serializes to the same string.
Matus Tomlein (matus-tomlein) pushed a commit that referenced this pull request Oct 25, 2024
The `addGlobalContexts` API now accepts an `object` mapping "names" to context primitives or context generators, rather than just an array of "unnamed" global entities/generators.

Calling `addGlobalContexts` multiple times with values of the same "name" will overwrite the previous value for subsequent events, allowing "upserting" or updating of global context without having to remove the previous version first and introducing duplicates.

`removeGlobalContexts` can now also accept plain strings which are handled as names, so primitives/generators can be removed by name without having to own a reference to the generating function or an object that serializes to the same string.
Matus Tomlein (matus-tomlein) pushed a commit that referenced this pull request Oct 28, 2024
The `addGlobalContexts` API now accepts an `object` mapping "names" to context primitives or context generators, rather than just an array of "unnamed" global entities/generators.

Calling `addGlobalContexts` multiple times with values of the same "name" will overwrite the previous value for subsequent events, allowing "upserting" or updating of global context without having to remove the previous version first and introducing duplicates.

`removeGlobalContexts` can now also accept plain strings which are handled as names, so primitives/generators can be removed by name without having to own a reference to the generating function or an object that serializes to the same string.
Matus Tomlein (matus-tomlein) pushed a commit that referenced this pull request Oct 28, 2024
The `addGlobalContexts` API now accepts an `object` mapping "names" to context primitives or context generators, rather than just an array of "unnamed" global entities/generators.

Calling `addGlobalContexts` multiple times with values of the same "name" will overwrite the previous value for subsequent events, allowing "upserting" or updating of global context without having to remove the previous version first and introducing duplicates.

`removeGlobalContexts` can now also accept plain strings which are handled as names, so primitives/generators can be removed by name without having to own a reference to the generating function or an object that serializes to the same string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants