Skip to content

Commit 5a3655b

Browse files
Create integrating-external-services.rst
1 parent c878cce commit 5a3655b

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#####################################################################
2+
How to: Integrating with third-party services for logging & analytics
3+
#####################################################################
4+
5+
Context
6+
***********************
7+
8+
Service classes
9+
==========
10+
11+
The ``frontend-platform`` library consists of several modules (e.g., logging, analytics, authentication, etc.) consumed by micro-frontends (MFEs) throughout the Open edX platform. The library has been architected in such a way that enables consumers to override default, base behavior of modules by overriding the default implementations of some module interfaces.
12+
13+
While modules (e.g., ``logging`` and ``analytics``) define base service classes, the ``initialize`` function exposes a mechanism for consumers to override the base service classes during MFE initialization, as needed to extend the library behavior or integrate with other external services.::
14+
15+
// override default services via ``initialize`` in MFE entrypoints
16+
initialize({
17+
loggingService = CustomLoggingService,
18+
analyticsService = CustomAnalyticsService,
19+
// ...
20+
});
21+
22+
// override default services via private ``env.config``
23+
const config = {
24+
loggingService: CustomLoggingService,
25+
analyticsService: CustomAnalyticsService,
26+
};
27+
export default config;
28+
29+
Each module with a service class exposes an interface and subsequently implements a base service class for the defined interface. For example, ``src/logging/interface.js`` defines the following interface:
30+
31+
- ``logInfo``
32+
- ``logError``
33+
- ``setCustomAttribute``
34+
35+
Regarding analytics, ``src/analytics/interface.js`` defines the service as having the following functions:
36+
- ``sendTrackingLogEvent``
37+
- ``identifyAuthenticatedUser``
38+
- ``identifyAnonymousUser``
39+
- ``sendTrackEvent``
40+
- ``sendPageEvent``
41+
42+
When overriding the base service classes (e.g., a custom ``LoggingService`` class), each of the required, defined interface functions must be implemented. The implementation of the interface is currently verified via ``PropTypes.checkPropTypes`` during the configuration/instantiation of the service class.
43+
44+
Other mechanisms to integrate third-party services
45+
==========
46+
47+
48+
``loadExternalScripts``
49+
------------
50+
51+
During MFE initialization, the ``initialize`` function accepts an optional ``externalScripts`` argument, representing a list of external "loader" classes instantiated via ``loadExternalScripts``::
52+
53+
initialize({
54+
externalScripts: [GoogleAnalyticsLoader],
55+
// ...
56+
})
57+
58+
By default, ``externalScripts`` contains a ``GoogleAnalyticsLoader``, but may be extended to include other loader classes, too.
59+
60+
Open questions:
61+
62+
#. Why isn't ``GoogleAnalyticsLoader`` treated as an overridden ``analyticsService``?
63+
#. Under what cirumstances *should* ``externalScripts`` be used versus overriding/extending the default ``loggingService`` vs. ``analyticsService``?
64+
#. Is it possible to use ``externalScripts`` without modifying ``openedx`` MFE code (e.g., does it require a fork?)
65+
#. Any scripts implemented via ``externalScripts`` will not be called via the service functions implemented throughout MFEs. For instance, what if an Open edX instance wants Google Analytics events for the instrumented ``sendTrackEvent`` usages throughout the platform?
66+
67+
(Proposed) ``useComponentPropOverrides`` & ``withComponentPropOverrides``
68+
------------
69+
70+
Neither of the above approaches to integrate third-party, external services into Open edX MFEs support passing vendor-specific HTML attributes or class names to specific rendered components. For example, to suppress/unsuppress PII from session replays in tools like Datadog/Hotjar, it necessitates passing a custom prop to the component that may not be hardcoded into ``openedx`` repositories (e.g., ``data-dd-privacy`` for Datadog).
71+
72+
A `proposed solution <https://github.com/openedx/frontend-platform/pull/723>`_ to this challenge is the addition of supporting the ability to override props for specific components via private configuration through the use of ``useComponentPropOverrides`` and/or ``withComponentPropOverrides``::
73+
74+
// example env.config
75+
const config = {
76+
componentPropOverrides: {
77+
targets: {
78+
example: {
79+
'data-dd-privacy': 'mask', // Custom `data-*` attribute (e.g., Datadog)
80+
onClick: (e) => console.log('custom event handler'),
81+
},
82+
},
83+
};
84+
export default config;
85+
86+
By default, ``useComponentPropOverrides`` and ``withComponentPropOverrides`` supports any custom `className` or `data-*` props. However, specific components may allow other prop types to be overridden applied by supplying the ``allowedPropNames`` and/or ``allowsDataAttributes`` options::
87+
88+
const ExampleComponentWithDefaultPropOverrides = ({ children, ...rest }) => {
89+
const propOverrides = useComponentPropOverrides('example', rest);
90+
return <span {...propOverrides}>{children}</span>;
91+
});
92+
93+
const ExampleComponentWithAllowedPropOverrides = ({ children, ...rest }) => {
94+
const propOverrides = useComponentPropOverrides('example', rest, {
95+
allowedPropNames: ['className', 'style', 'onClick'],
96+
allowsDataAttributes: true,
97+
});
98+
return <span {...propOverrides}>{children}</span>;
99+
});

0 commit comments

Comments
 (0)