UNPKG

6.74 kBPlain TextView Raw
1/*
2 * Copyright (c) 2021 Snowplow Analytics Ltd, 2010 Anthon Pang
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31import * as ClientHints from '@snowplow/browser-plugin-client-hints';
32import * as Optimizely from '@snowplow/browser-plugin-optimizely';
33import * as OptimizelyX from '@snowplow/browser-plugin-optimizely-x';
34import * as PerformanceTiming from '@snowplow/browser-plugin-performance-timing';
35import * as Consent from '@snowplow/browser-plugin-consent';
36import * as Geolocation from '@snowplow/browser-plugin-geolocation';
37import * as GaCookies from '@snowplow/browser-plugin-ga-cookies';
38import * as LinkClickTracking from '@snowplow/browser-plugin-link-click-tracking';
39import * as FormTracking from '@snowplow/browser-plugin-form-tracking';
40import * as ErrorTracking from '@snowplow/browser-plugin-error-tracking';
41import * as BrowserFeatures from '@snowplow/browser-plugin-browser-features';
42import * as Timezone from '@snowplow/browser-plugin-timezone';
43import * as Ecommerce from '@snowplow/browser-plugin-ecommerce';
44import * as EnhancedEcommerce from '@snowplow/browser-plugin-enhanced-ecommerce';
45import * as AdTracking from '@snowplow/browser-plugin-ad-tracking';
46import * as SiteTracking from '@snowplow/browser-plugin-site-tracking';
47import { plugins } from '../tracker.config';
48import { BrowserPlugin } from '@snowplow/browser-tracker-core';
49import { JavaScriptTrackerConfiguration } from './configuration';
50
51/**
52 * Calculates the required plugins to intialise per tracker
53 * @param configuration - The tracker configuration object
54 */
55export function Plugins(configuration: JavaScriptTrackerConfiguration) {
56 const {
57 performanceTiming,
58 gaCookies,
59 geolocation,
60 optimizelyExperiments,
61 optimizelyStates,
62 optimizelyVariations,
63 optimizelyVisitor,
64 optimizelyAudiences,
65 optimizelyDimensions,
66 optimizelySummary,
67 optimizelyXSummary,
68 clientHints,
69 } = configuration?.contexts ?? {};
70 const activatedPlugins: Array<[BrowserPlugin, {} | Record<string, Function>]> = [];
71
72 if (
73 plugins.optimizely &&
74 (optimizelySummary ||
75 optimizelyExperiments ||
76 optimizelyStates ||
77 optimizelyVariations ||
78 optimizelyVisitor ||
79 optimizelyAudiences ||
80 optimizelyDimensions)
81 ) {
82 const { OptimizelyPlugin, ...apiMethods } = Optimizely;
83 activatedPlugins.push([
84 OptimizelyPlugin(
85 optimizelySummary,
86 optimizelyExperiments,
87 optimizelyStates,
88 optimizelyVariations,
89 optimizelyVisitor,
90 optimizelyAudiences,
91 optimizelyDimensions
92 ),
93 apiMethods,
94 ]);
95 }
96
97 if (plugins.performanceTiming && performanceTiming) {
98 const { PerformanceTimingPlugin, ...apiMethods } = PerformanceTiming;
99 activatedPlugins.push([PerformanceTimingPlugin(), apiMethods]);
100 }
101
102 if (plugins.optimizelyX && optimizelyXSummary) {
103 const { OptimizelyXPlugin, ...apiMethods } = OptimizelyX;
104 activatedPlugins.push([OptimizelyXPlugin(), apiMethods]);
105 }
106
107 if (plugins.clientHints && clientHints) {
108 const { ClientHintsPlugin, ...apiMethods } = ClientHints;
109 activatedPlugins.push([
110 ClientHintsPlugin(typeof clientHints === 'object' && clientHints.includeHighEntropy),
111 apiMethods,
112 ]);
113 }
114
115 if (plugins.gaCookies && gaCookies) {
116 const { GaCookiesPlugin, ...apiMethods } = GaCookies;
117 activatedPlugins.push([GaCookiesPlugin(), apiMethods]);
118 }
119
120 if (plugins.consent) {
121 const { ConsentPlugin, ...apiMethods } = Consent;
122 activatedPlugins.push([ConsentPlugin(), apiMethods]);
123 }
124
125 if (plugins.geolocation) {
126 const { GeolocationPlugin, ...apiMethods } = Geolocation;
127 activatedPlugins.push([GeolocationPlugin(geolocation), apiMethods]);
128 }
129
130 if (plugins.linkClickTracking) {
131 const { LinkClickTrackingPlugin, ...apiMethods } = LinkClickTracking;
132 activatedPlugins.push([LinkClickTrackingPlugin(), apiMethods]);
133 }
134
135 if (plugins.formTracking) {
136 const { FormTrackingPlugin, ...apiMethods } = FormTracking;
137 activatedPlugins.push([FormTrackingPlugin(), apiMethods]);
138 }
139
140 if (plugins.errorTracking) {
141 const { ErrorTrackingPlugin, ...apiMethods } = ErrorTracking;
142 activatedPlugins.push([ErrorTrackingPlugin(), apiMethods]);
143 }
144
145 if (plugins.ecommerce) {
146 const { EcommercePlugin, ...apiMethods } = Ecommerce;
147 activatedPlugins.push([EcommercePlugin(), apiMethods]);
148 }
149
150 if (plugins.enhancedEcommerce) {
151 const { EnhancedEcommercePlugin, ...apiMethods } = EnhancedEcommerce;
152 activatedPlugins.push([EnhancedEcommercePlugin(), apiMethods]);
153 }
154
155 if (plugins.adTracking) {
156 const { AdTrackingPlugin, ...apiMethods } = AdTracking;
157 activatedPlugins.push([AdTrackingPlugin(), apiMethods]);
158 }
159
160 if (plugins.siteTracking) {
161 const { SiteTrackingPlugin, ...apiMethods } = SiteTracking;
162 activatedPlugins.push([SiteTrackingPlugin(), apiMethods]);
163 }
164
165 if (plugins.browserFeatures) {
166 const { BrowserFeaturesPlugin, ...apiMethods } = BrowserFeatures;
167 activatedPlugins.push([BrowserFeaturesPlugin(), apiMethods]);
168 }
169
170 if (plugins.timezone) {
171 const { TimezonePlugin, ...apiMethods } = Timezone;
172 activatedPlugins.push([TimezonePlugin(), apiMethods]);
173 }
174
175 return activatedPlugins;
176}