|
| 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 | + |
| 37 | +- ``sendTrackingLogEvent`` |
| 38 | +- ``identifyAuthenticatedUser`` |
| 39 | +- ``identifyAnonymousUser`` |
| 40 | +- ``sendTrackEvent`` |
| 41 | +- ``sendPageEvent`` |
| 42 | + |
| 43 | +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. |
| 44 | + |
| 45 | +Other mechanisms to integrate third-party services |
| 46 | +========== |
| 47 | + |
| 48 | + |
| 49 | +``loadExternalScripts`` |
| 50 | +------------ |
| 51 | + |
| 52 | +During MFE initialization, the ``initialize`` function accepts an optional ``externalScripts`` argument, representing a list of external "loader" classes instantiated via ``loadExternalScripts``:: |
| 53 | + |
| 54 | + initialize({ |
| 55 | + externalScripts: [GoogleAnalyticsLoader], |
| 56 | + // ... |
| 57 | + }) |
| 58 | + |
| 59 | +By default, ``externalScripts`` contains a ``GoogleAnalyticsLoader``, but may be extended to include other loader classes, too. |
| 60 | + |
| 61 | +Open questions: |
| 62 | + |
| 63 | +#. Why isn't ``GoogleAnalyticsLoader`` treated as an overridden ``analyticsService``? |
| 64 | +#. Under what cirumstances *should* ``externalScripts`` be used versus overriding/extending the default ``loggingService`` vs. ``analyticsService``? |
| 65 | +#. Is it possible to use ``externalScripts`` without modifying ``openedx`` MFE code (e.g., does it require a fork?) |
| 66 | +#. 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? |
| 67 | + |
| 68 | +(Proposed) ``useComponentPropOverrides`` & ``withComponentPropOverrides`` |
| 69 | +------------ |
| 70 | + |
| 71 | +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). |
| 72 | + |
| 73 | +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``:: |
| 74 | + |
| 75 | + // example env.config |
| 76 | + const config = { |
| 77 | + componentPropOverrides: { |
| 78 | + targets: { |
| 79 | + example: { |
| 80 | + 'data-dd-privacy': 'mask', // Custom `data-*` attribute (e.g., Datadog) |
| 81 | + onClick: (e) => console.log('custom event handler'), |
| 82 | + }, |
| 83 | + }, |
| 84 | + }; |
| 85 | + export default config; |
| 86 | + |
| 87 | +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:: |
| 88 | + |
| 89 | + const ExampleComponentWithDefaultPropOverrides = ({ children, ...rest }) => { |
| 90 | + const propOverrides = useComponentPropOverrides('example', rest); |
| 91 | + return <span {...propOverrides}>{children}</span>; |
| 92 | + }); |
| 93 | + |
| 94 | + const ExampleComponentWithAllowedPropOverrides = ({ children, ...rest }) => { |
| 95 | + const propOverrides = useComponentPropOverrides('example', rest, { |
| 96 | + allowedPropNames: ['className', 'style', 'onClick'], |
| 97 | + allowsDataAttributes: true, |
| 98 | + }); |
| 99 | + return <span {...propOverrides}>{children}</span>; |
| 100 | + }); |
| 101 | + |
| 102 | +Performance considerations |
| 103 | +*********************** |
| 104 | + |
| 105 | +Performance implications should be considered while integrating with other services.Any third-party, external vendor scripts (e.g., Segment, Google Analytics, Datadog, etc.) loaded via a service class (e.g., ``SegmentAnalyticsService``, ``externalScripts``) are injecting ``<script>`` tags into the HTML document *after* much of the MFE's application code has been downloaded/parsed. That is, all JS needed for frontend-platform's MFE initialization must be downloaded/evaluated first *before* downloading any external scripts. |
| 106 | + |
| 107 | +The default/base ``NewRelicLoggingService`` is currently an exception in that it injects the New Relic JS `<script>` tag during the Webpack build process via ``HtmlWebpackNewRelicPlugin``, whereby the New Relic `<script>` may be downloaded earlier in the initial page load, without being blocked on parsing frontend-platform's JS. In a similar vein, many MFEs integrate with Optimizely by hardcoding its associated ``<script>`` element in the ``public/index.html`` file for the MFE (not open-source friendly). |
0 commit comments