@mparticle/web-google-analytics-4-server-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # mParticle Google Analytis 4 Server Side Kit Integration
2
+
3
+ Google's server side API for GA4 requires a `client_id` which still necessitates loading Google's SDK. In this kit, mParticle loads the SDK, collects the `client_id`, and sends this as an integration attribute to our server to include it on events sent to GA4.
4
+
5
+ # Usage
6
+ JS kits are automatically included with your mParticle.js file when loading mParticle via the [snippet](https://docs.mparticle.com/developers/sdk/web/getting-started/#add-the-sdk-snippet).
7
+
8
+ If loading mParticle via [npm](https://docs.mparticle.com/developers/sdk/web/self-hosting/), you will have to manually include the server side GA4 kit via npm:
9
+
10
+ ```
11
+ npm i @mparticle/web-google-analytics-4-server-kit
12
+ ```
13
+
14
+ Full documentation on usage can be found [here](https://docs.mparticle.com/integrations/google-analytics-4/event/).
15
+
16
+ #License
17
+
18
+ Copyright 2022 mParticle, Inc.
19
+
20
+ Licensed under the Apache License, Version 2.0 (the "License");
21
+ you may not use this file except in compliance with the License.
22
+ You may obtain a copy of the License at
23
+
24
+ http://www.apache.org/licenses/LICENSE-2.0
25
+
26
+ Unless required by applicable law or agreed to in writing, software
27
+ distributed under the License is distributed on an "AS IS" BASIS,
28
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ See the License for the specific language governing permissions and
30
+ limitations under the License.
@@ -0,0 +1,129 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+
3
+ /* eslint-disable no-undef */
4
+
5
+ //
6
+ // Copyright 2022 mParticle, Inc.
7
+ //
8
+ // Licensed under the Apache License, Version 2.0 (the 'License');
9
+ // you may not use this file except in compliance with the License.
10
+ // You may obtain a copy of the License at
11
+ //
12
+ // http://www.apache.org/licenses/LICENSE-2.0
13
+ //
14
+ // Unless required by applicable law or agreed to in writing, software
15
+ // distributed under the License is distributed on an 'AS IS' BASIS,
16
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ // See the License for the specific language governing permissions and
18
+ // limitations under the License.
19
+
20
+
21
+ // This GA4 server side kit loads gtag, gets the client id, and sets it as an integration attribute on the core sdk.
22
+ // This kit does not have a `processEvent` method because no events should be sent client side.
23
+
24
+ var name = 'GoogleAnalytics4EventForwarder',
25
+ GA4MODULENUMBER = 160;
26
+
27
+ var constructor = function() {
28
+ var self = this;
29
+ self.name = name;
30
+
31
+ function initForwarder(forwarderSettings, service, testMode) {
32
+ mParticle._setIntegrationDelay(GA4MODULENUMBER, true);
33
+
34
+ var measurementId = forwarderSettings.measurementId;
35
+ window.dataLayer = window.dataLayer || [];
36
+
37
+ window.gtag = function() {
38
+ window.dataLayer.push(arguments);
39
+ };
40
+
41
+ if (!testMode) {
42
+ var clientScript = document.createElement('script');
43
+ clientScript.type = 'text/javascript';
44
+ clientScript.async = true;
45
+ clientScript.src =
46
+ 'https://www.googletagmanager.com/gtag/js?id=' + measurementId;
47
+ (
48
+ document.getElementsByTagName('head')[0] ||
49
+ document.getElementsByTagName('body')[0]
50
+ ).appendChild(clientScript);
51
+
52
+ clientScript.onload = function() {
53
+ gtag('js', new Date());
54
+
55
+ gtag('get', measurementId, 'client_id', function(clientId) {
56
+ setClientId(clientId, GA4MODULENUMBER);
57
+ });
58
+ return;
59
+ };
60
+ }
61
+ }
62
+
63
+ this.init = initForwarder;
64
+ };
65
+
66
+ function setClientId(clientId, GA4MODULENUMBER) {
67
+ var GA4CLIENTID = 'client_id';
68
+ var ga4IntegrationAttributes = {};
69
+ ga4IntegrationAttributes[GA4CLIENTID] = clientId;
70
+ mParticle.setIntegrationAttribute(
71
+ GA4MODULENUMBER,
72
+ ga4IntegrationAttributes
73
+ );
74
+ mParticle._setIntegrationDelay(GA4MODULENUMBER, false);
75
+ }
76
+
77
+ function getId() {
78
+ return GA4MODULENUMBER;
79
+ }
80
+
81
+ if (window && window.mParticle && window.mParticle.addForwarder) {
82
+ window.mParticle.addForwarder({
83
+ name: name,
84
+ constructor: constructor,
85
+ getId: getId,
86
+ });
87
+ }
88
+
89
+ function register(config) {
90
+ if (!config) {
91
+ window.console.log(
92
+ 'You must pass a config object to register the kit ' + name
93
+ );
94
+ return;
95
+ }
96
+
97
+ if (!isObject(config)) {
98
+ window.console.log(
99
+ "'config' must be an object. You passed in a " + typeof config
100
+ );
101
+ return;
102
+ }
103
+
104
+ if (isObject(config.kits)) {
105
+ config.kits[name] = {
106
+ constructor: constructor,
107
+ };
108
+ } else {
109
+ config.kits = {};
110
+ config.kits[name] = {
111
+ constructor: constructor,
112
+ };
113
+ }
114
+ window.console.log(
115
+ 'Successfully registered ' + name + ' to your mParticle configuration'
116
+ );
117
+ }
118
+
119
+ function isObject(val) {
120
+ return (
121
+ val != null && typeof val === 'object' && Array.isArray(val) === false
122
+ );
123
+ }
124
+
125
+ var GoogleAnalytics4EventForwarderServerSide = {
126
+ register: register,
127
+ };
128
+
129
+ exports.default = GoogleAnalytics4EventForwarderServerSide;
@@ -0,0 +1,132 @@
1
+ var mParticleGA4 = (function (exports) {
2
+ /* eslint-disable no-undef */
3
+
4
+ //
5
+ // Copyright 2022 mParticle, Inc.
6
+ //
7
+ // Licensed under the Apache License, Version 2.0 (the 'License');
8
+ // you may not use this file except in compliance with the License.
9
+ // You may obtain a copy of the License at
10
+ //
11
+ // http://www.apache.org/licenses/LICENSE-2.0
12
+ //
13
+ // Unless required by applicable law or agreed to in writing, software
14
+ // distributed under the License is distributed on an 'AS IS' BASIS,
15
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ // See the License for the specific language governing permissions and
17
+ // limitations under the License.
18
+
19
+
20
+ // This GA4 server side kit loads gtag, gets the client id, and sets it as an integration attribute on the core sdk.
21
+ // This kit does not have a `processEvent` method because no events should be sent client side.
22
+
23
+ var name = 'GoogleAnalytics4EventForwarder',
24
+ GA4MODULENUMBER = 160;
25
+
26
+ var constructor = function() {
27
+ var self = this;
28
+ self.name = name;
29
+
30
+ function initForwarder(forwarderSettings, service, testMode) {
31
+ mParticle._setIntegrationDelay(GA4MODULENUMBER, true);
32
+
33
+ var measurementId = forwarderSettings.measurementId;
34
+ window.dataLayer = window.dataLayer || [];
35
+
36
+ window.gtag = function() {
37
+ window.dataLayer.push(arguments);
38
+ };
39
+
40
+ if (!testMode) {
41
+ var clientScript = document.createElement('script');
42
+ clientScript.type = 'text/javascript';
43
+ clientScript.async = true;
44
+ clientScript.src =
45
+ 'https://www.googletagmanager.com/gtag/js?id=' + measurementId;
46
+ (
47
+ document.getElementsByTagName('head')[0] ||
48
+ document.getElementsByTagName('body')[0]
49
+ ).appendChild(clientScript);
50
+
51
+ clientScript.onload = function() {
52
+ gtag('js', new Date());
53
+
54
+ gtag('get', measurementId, 'client_id', function(clientId) {
55
+ setClientId(clientId, GA4MODULENUMBER);
56
+ });
57
+ return;
58
+ };
59
+ }
60
+ }
61
+
62
+ this.init = initForwarder;
63
+ };
64
+
65
+ function setClientId(clientId, GA4MODULENUMBER) {
66
+ var GA4CLIENTID = 'client_id';
67
+ var ga4IntegrationAttributes = {};
68
+ ga4IntegrationAttributes[GA4CLIENTID] = clientId;
69
+ mParticle.setIntegrationAttribute(
70
+ GA4MODULENUMBER,
71
+ ga4IntegrationAttributes
72
+ );
73
+ mParticle._setIntegrationDelay(GA4MODULENUMBER, false);
74
+ }
75
+
76
+ function getId() {
77
+ return GA4MODULENUMBER;
78
+ }
79
+
80
+ if (window && window.mParticle && window.mParticle.addForwarder) {
81
+ window.mParticle.addForwarder({
82
+ name: name,
83
+ constructor: constructor,
84
+ getId: getId,
85
+ });
86
+ }
87
+
88
+ function register(config) {
89
+ if (!config) {
90
+ window.console.log(
91
+ 'You must pass a config object to register the kit ' + name
92
+ );
93
+ return;
94
+ }
95
+
96
+ if (!isObject(config)) {
97
+ window.console.log(
98
+ "'config' must be an object. You passed in a " + typeof config
99
+ );
100
+ return;
101
+ }
102
+
103
+ if (isObject(config.kits)) {
104
+ config.kits[name] = {
105
+ constructor: constructor,
106
+ };
107
+ } else {
108
+ config.kits = {};
109
+ config.kits[name] = {
110
+ constructor: constructor,
111
+ };
112
+ }
113
+ window.console.log(
114
+ 'Successfully registered ' + name + ' to your mParticle configuration'
115
+ );
116
+ }
117
+
118
+ function isObject(val) {
119
+ return (
120
+ val != null && typeof val === 'object' && Array.isArray(val) === false
121
+ );
122
+ }
123
+
124
+ var GoogleAnalytics4EventForwarderServerSide = {
125
+ register: register,
126
+ };
127
+
128
+ exports.default = GoogleAnalytics4EventForwarderServerSide;
129
+
130
+ return exports;
131
+
132
+ }({}));
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@mparticle/web-google-analytics-4-server-kit",
3
+ "version": "1.0.0",
4
+ "description": "mParticle integration sdk for Google Analytics (server side)",
5
+ "main": "dist/GoogleAnalytics4EventForwarderServerSide-Kit.common.js",
6
+ "browser": "dist/GoogleAnalytics4EventForwarderServerSide-Kit.iife.js",
7
+ "scripts": {
8
+ "build": "rollup --config rollup.config.js",
9
+ "watch": "rollup --config rollup.config.js -w"
10
+ },
11
+ "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
12
+ "license": "Apache-2.0",
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-google-analytics-4/tree/master/packages/GA4Server",
17
+ "devDependencies": {
18
+ "rollup": "^1.15.6"
19
+ }
20
+ }
@@ -0,0 +1,31 @@
1
+ const input = 'src/GoogleAnalytics4EventForwarderServerSide.js';
2
+
3
+ const outputOptions = {
4
+ exports: 'named',
5
+ strict: false,
6
+ };
7
+
8
+ const builds = {
9
+ // for dynamic script tag loading of GA4 server side kit
10
+ server_iife: {
11
+ input,
12
+ output: {
13
+ ...outputOptions,
14
+ name: 'mParticleGA4',
15
+ file: 'dist/GoogleAnalytics4EventForwarderServerSide-Kit.iife.js',
16
+ format: 'iife',
17
+ },
18
+ },
19
+ // creates npm module for GA4 server side kit
20
+ server_cjs: {
21
+ input,
22
+ output: {
23
+ ...outputOptions,
24
+ name: 'mParticleGA4',
25
+ file: 'dist/GoogleAnalytics4EventForwarderServerSide-Kit.common.js',
26
+ format: 'cjs',
27
+ },
28
+ },
29
+ };
30
+
31
+ export default [builds.server_iife, builds.server_cjs];
@@ -0,0 +1,125 @@
1
+ /* eslint-disable no-undef */
2
+
3
+ //
4
+ // Copyright 2022 mParticle, Inc.
5
+ //
6
+ // Licensed under the Apache License, Version 2.0 (the 'License');
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an 'AS IS' BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+
18
+
19
+ // This GA4 server side kit loads gtag, gets the client id, and sets it as an integration attribute on the core sdk.
20
+ // This kit does not have a `processEvent` method because no events should be sent client side.
21
+
22
+ var name = 'GoogleAnalytics4EventForwarder',
23
+ GA4MODULENUMBER = 160;
24
+
25
+ var constructor = function() {
26
+ var self = this;
27
+ self.name = name;
28
+
29
+ function initForwarder(forwarderSettings, service, testMode) {
30
+ mParticle._setIntegrationDelay(GA4MODULENUMBER, true);
31
+
32
+ var measurementId = forwarderSettings.measurementId;
33
+ window.dataLayer = window.dataLayer || [];
34
+
35
+ window.gtag = function() {
36
+ window.dataLayer.push(arguments);
37
+ };
38
+
39
+ if (!testMode) {
40
+ var clientScript = document.createElement('script');
41
+ clientScript.type = 'text/javascript';
42
+ clientScript.async = true;
43
+ clientScript.src =
44
+ 'https://www.googletagmanager.com/gtag/js?id=' + measurementId;
45
+ (
46
+ document.getElementsByTagName('head')[0] ||
47
+ document.getElementsByTagName('body')[0]
48
+ ).appendChild(clientScript);
49
+
50
+ clientScript.onload = function() {
51
+ gtag('js', new Date());
52
+
53
+ gtag('get', measurementId, 'client_id', function(clientId) {
54
+ setClientId(clientId, GA4MODULENUMBER);
55
+ });
56
+ return;
57
+ };
58
+ }
59
+ }
60
+
61
+ this.init = initForwarder;
62
+ };
63
+
64
+ function setClientId(clientId, GA4MODULENUMBER) {
65
+ var GA4CLIENTID = 'client_id';
66
+ var ga4IntegrationAttributes = {};
67
+ ga4IntegrationAttributes[GA4CLIENTID] = clientId;
68
+ mParticle.setIntegrationAttribute(
69
+ GA4MODULENUMBER,
70
+ ga4IntegrationAttributes
71
+ );
72
+ mParticle._setIntegrationDelay(GA4MODULENUMBER, false);
73
+ }
74
+
75
+ function getId() {
76
+ return GA4MODULENUMBER;
77
+ }
78
+
79
+ if (window && window.mParticle && window.mParticle.addForwarder) {
80
+ window.mParticle.addForwarder({
81
+ name: name,
82
+ constructor: constructor,
83
+ getId: getId,
84
+ });
85
+ }
86
+
87
+ function register(config) {
88
+ if (!config) {
89
+ window.console.log(
90
+ 'You must pass a config object to register the kit ' + name
91
+ );
92
+ return;
93
+ }
94
+
95
+ if (!isObject(config)) {
96
+ window.console.log(
97
+ "'config' must be an object. You passed in a " + typeof config
98
+ );
99
+ return;
100
+ }
101
+
102
+ if (isObject(config.kits)) {
103
+ config.kits[name] = {
104
+ constructor: constructor,
105
+ };
106
+ } else {
107
+ config.kits = {};
108
+ config.kits[name] = {
109
+ constructor: constructor,
110
+ };
111
+ }
112
+ window.console.log(
113
+ 'Successfully registered ' + name + ' to your mParticle configuration'
114
+ );
115
+ }
116
+
117
+ function isObject(val) {
118
+ return (
119
+ val != null && typeof val === 'object' && Array.isArray(val) === false
120
+ );
121
+ }
122
+
123
+ export default {
124
+ register: register,
125
+ };