Skip to content

Commit aeeeb50

Browse files
committed
[netlify-build]
1 parent d47116d commit aeeeb50

File tree

1 file changed

+31
-3
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+31
-3
lines changed

src/connections/sources/catalog/libraries/website/javascript/index.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ No, there is no impact to how events filter.
724724
## Plugin Architecture
725725
When you develop against Analytics 2.0, the plugins you write can augment functionality, enrich data, and control the flow and delivery of events. From modifying event payloads to changing analytics functionality, plugins help to speed up the process of getting things done.
726726

727+
Though middlewares function the same as plugins, it's best to use plugins as they are easier to implement and are more testable.
728+
727729
### Plugin Categories
728730
Plugins are bound by Analytics 2.0 which handles operations such as observability, retries, and error handling. There are two different categories of plugins:
729731
* **Critical Plugins**: Analytics.js expects this plugin to be loaded before starting event delivery. Failure to load a critical plugin halts event delivery. Use this category sparingly, and only for plugins that are critical to your tracking.
@@ -740,7 +742,7 @@ Type | Details
740742
`enrichment` | Executes as the first level of event processing. These plugins modify an event. <br><br> See the example of how Analytics.js uses the [Page Enrichment plugin](https://github.com/segmentio/analytics-next/blob/master/src/plugins/page-enrichment/index.ts){:target="_blank"} to enrich every event with page information.
741743
`destination` | Executes as events begin to pass off to destinations. <br><br> This doesn’t modify the event outside of the specific destination, and failure doesn’t halt the execution.
742744
`after` | Executes after all event processing completes. You can use this to perform cleanup operations. <br><br>An example of this is the [Segment.io Plugin](https://github.com/segmentio/analytics-next/blob/master/src/plugins/segmentio/index.ts){:target="_blank"} which waits for destinations to succeed or fail so it can send it observability metrics.
743-
`utility` | Executes only with manual calls such as Logging. <br><br>These are plugins that change the Analytics 2.0 functionality. Utility plugins run outside of the event pipeline, and only execute once.
745+
`utility` | Executes once during the bootstrap, to give you an outlet to make any modifications as to how Analytics.js works internally. This allows you to augment Analytics.js functionality.
744746

745747
### Example Plugins
746748
Here's an example of a plugin that converts all track event names to lowercase before the event goes through the rest of the pipeline:
@@ -810,14 +812,40 @@ const identityStitching = () => {
810812
await window.analytics.register(identityStitching())
811813
```
812814

815+
Here's an example of a `utility` plugin that allows you to change the format of the anonymous_id cookie:
816+
817+
```js
818+
819+
window.analytics.ready(() => {
820+
window.analytics.register({
821+
name: 'Cookie Compatibility',
822+
version: '0.1.0',
823+
type: 'utility',
824+
load: (_ctx, ajs) => {
825+
const user = ajs.user()
826+
const cookieJar = user.cookies
827+
const cookieSetter = cookieJar.set.bind(cookieJar)
828+
829+
// blindly convert any values into JSON strings
830+
cookieJar.set = (key, value, opts) => cookieSetter(key, JSON.stringify(value), opts)
831+
832+
// stringify any existing IDs
833+
user.anonymousId(user.anonymousId())
834+
user.id(user.id())
835+
},
836+
isLoaded: () => true
837+
})
838+
})
839+
```
840+
813841
You can view Segment's [existing plugins](https://github.com/segmentio/analytics-next/tree/master/src/plugins){:target="_blank"} to see more examples.
814842

815843
### Register a plugin
816844
Registering plugins enable you to modify your analytics implementation to best fit your needs. You can register a plugin using this:
817845

818846
```js
819-
// A promise will resolve once the plugins has been successfully loaded into Analytics.js
820-
// You can register multiple plugins at once by using the variable args interface in Analytis.js
847+
// A promise will resolve once the plugins have been successfully loaded into Analytics.js
848+
// You can register multiple plugins at once by using the variable args interface in Analytics.js
821849
await window.analytics.register(pluginA, pluginB, pluginN)
822850
```
823851

0 commit comments

Comments
 (0)