gatsby-plugin-rudderstack 1.0.4 → 1.1.1

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 CHANGED
@@ -3,21 +3,21 @@
3
3
  <p align="center"><b>The warehouse-first customer data platform built for devs.</b></p>
4
4
  <br/>
5
5
 
6
- # The Rudderstack Plugin for Gatsby.
6
+ # The RudderStack Plugin for Gatsby.
7
7
 
8
- This is a way for you to quickly and easily get Rudderstack up and running in your Gatsby application.
8
+ This is a way for you to quickly and easily get RudderStack up and running in your Gatsby application.
9
9
 
10
10
  Questions? Please join our [Slack channel](https://resources.rudderstack.com/join-rudderstack-slack) or read about us on [Product Hunt](https://www.producthunt.com/posts/rudderstack).
11
11
 
12
12
  # Why Use This plugin
13
13
 
14
- This plugin makes it easy to integrate your Gatsby website with the Rudderstack API and easily track events.
14
+ This plugin makes it easy to integrate your Gatsby website with the RudderStack API and easily track events.
15
15
 
16
16
  # Key Features
17
17
 
18
18
  - use multiple write keys (one for prod env, another optional one for dev)
19
19
  - disable page view tracking (just in case you want to add it later manually)
20
- - up to date (Rudderstack team maintained)
20
+ - up to date (RudderStack team maintained)
21
21
 
22
22
  ## Install
23
23
 
@@ -58,24 +58,23 @@ plugins: [
58
58
 
59
59
  // If you need to proxy events through a custom data plane,
60
60
  // add a `dataPlaneUrl` property (defaults to https://hosted.rudderlabs.com )
61
- // Rudderstack docs:
61
+ // RudderStack docs:
62
62
  // - https://docs.rudderstack.com/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk#3-1-load
63
63
  dataPlaneUrl: `https://override-rudderstack-endpoint`,
64
64
 
65
65
  // Add a `controlPlaneUrl` property if you are self-hosting the Control Plane
66
- // Rudderstack docs:
66
+ // RudderStack docs:
67
67
  // - https://docs.rudderstack.com/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk#3-1-1-self-hosted-control-plane
68
68
  controlPlaneUrl: `https://override-control-plane-url`,
69
69
 
70
- // boolean (defaults to false); whether to delay load Rudderstack
70
+ // boolean (defaults to false); whether to delay load RudderStack
71
71
  // ADVANCED FEATURE: only use if you leverage client-side routing (ie, Gatsby <Link>)
72
- // This feature will force Rudderstack to load _after_ either a page routing change
73
- // or user scroll, whichever comes first. This delay time is controlled by
74
- // `delayLoadTime` setting. This feature is used to help improve your website's
72
+ // This feature will force RudderStack to load _after_ either a page routing change
73
+ // or user scroll, or after `delayLoadTime` elapses, whichever comes first. This feature is used to help improve your website's
75
74
  // TTI (for SEO, UX, etc). See links below for more info.
76
75
  // NOTE: But if you are using server-side routing and enable this feature,
77
- // Rudderstack will never load (because although client-side routing does not do
78
- // a full page refresh, server-side routing does, thereby preventing Rudderstack
76
+ // RudderStack will never load (because although client-side routing does not do
77
+ // a full page refresh, server-side routing does, thereby preventing RudderStack
79
78
  // from ever loading).
80
79
  // See here for more context:
81
80
  // GIF: https://github.com/benjaminhoffman/gatsby-plugin-segment/pull/19#issuecomment-559569483
@@ -83,9 +82,10 @@ plugins: [
83
82
  // Problem/solution: https://marketingexamples.com/seo/performance
84
83
  delayLoad: false,
85
84
 
86
- // number (default to 1000); time to wait after scroll or route change
85
+ // number (default to 1000); time to wait after the page loads
86
+ // to load the RudderStack SDK
87
87
  // To be used when `delayLoad` is set to `true`
88
- delayLoadTime: 1000
88
+ delayLoadTime: 1000,
89
89
 
90
90
  // Whether to completely skip calling `analytics.load()`.
91
91
  // ADVANCED FEATURE: only use if you are calling `analytics.load()` manually
@@ -93,13 +93,19 @@ plugins: [
93
93
  // that will call it for you.
94
94
  // Useful for only loading the tracking script once a user has opted in to being tracked, for example.
95
95
  // *Another use case is if you want to add callbacks to the methods at load time.
96
- manualLoad: false
96
+ manualLoad: false,
97
97
 
98
98
  // string ('async' or 'defer'); whether to load the RudderStack SDK async or defer. Anything else
99
99
  // will load normally.
100
100
  // 'async' will load the SDK as <script async></script>
101
101
  // 'defer' will load the SDK as <script defer></script>
102
- loadType: 'default'
102
+ loadType: 'default',
103
+
104
+ // Options to the `load` API
105
+ // Note: The above `controlPlaneUrl` overrides the `configUrl` field in this object
106
+ loadOptions: {
107
+ ...
108
+ }
103
109
  }
104
110
  }
105
111
  ];
@@ -191,6 +197,8 @@ exports.onRouteUpdate = () => {
191
197
  };
192
198
  ```
193
199
 
200
+ Note: The above code snippet might not give the best results all the time as it doesn't take various SDK loading scenarios into consideration. Hence, if you are not tracking page views automatically, then clone the contents of the `gatsby-browser.js` from the package and make necessary modifications.
201
+
194
202
  You’ve now successfully installed `rudder-analytics.js` tracking. You can enable and use any event destination to send your event data via RudderStack, in no time at all!
195
203
 
196
204
  ## [](https://github.com/rudderlabs/rudder-sdk-js/blob/master/README.md#step-4-check-ready-state)Step 4: Check Ready State
package/gatsby-browser.js CHANGED
@@ -6,22 +6,48 @@ exports.onRouteUpdate = function (_ref, _ref2) {
6
6
  _ref2$trackPageDelay = _ref2.trackPageDelay,
7
7
  trackPageDelay = _ref2$trackPageDelay === undefined ? 50 : _ref2$trackPageDelay;
8
8
 
9
- function trackRudderstackPage() {
10
- var delay = Math.max(0, trackPageDelay);
9
+ function trackRudderStackPage() {
10
+ if (trackPage) {
11
+ // Adding a delay (defaults to 50ms when not provided by plugin option `trackPageDelay`)
12
+ // ensure that the RudderStack route tracking is in sync with the actual Gatsby route
13
+ // (otherwise you can end up in a state where the RudderStack page tracking reports
14
+ // the previous page on route change).
15
+ var delay = Math.max(0, trackPageDelay);
11
16
 
12
- window.setTimeout(function () {
13
- if (trackPage) {
17
+ window.setTimeout(function () {
14
18
  window.rudderanalytics && window.rudderanalytics.page(document.title);
15
- }
16
- }, delay);
19
+ }, delay);
20
+ }
17
21
  }
18
22
 
19
- // Track only non-home page views
20
- if (prevLocation) {
21
- if (window.rudderSnippetLoaded === false && window.rudderSnippetLoading === false) {
22
- window.rudderSnippetLoader();
23
+ // IMPORTANT: If you are cloning the contents of this file,
24
+ // we recommend to keep this section of the code intact and
25
+ // just change the tracking logic in `trackRudderStackPage` method.
26
+ if (window.rudderSnippetLoaded === false) {
27
+ if (window.rudderSnippetLoading === true) {
28
+ // As the loading is in progress, set the alternate callback function
29
+ // to track page
30
+ window.rudderSnippetLoadedCallback = function () {
31
+ trackRudderStackPage();
32
+ };
23
33
  } else {
24
- trackRudderstackPage();
34
+ // if it is not the first page
35
+ if (prevLocation) {
36
+ // Trigger the script loader and set the callback function
37
+ // to track page
38
+ window.rudderSnippetLoadedCallback = undefined;
39
+ window.rudderSnippetLoader(function () {
40
+ trackRudderStackPage();
41
+ });
42
+ } else {
43
+ // As this is the first page, set the alternate callback function
44
+ // to track page and wait for the scroll event to occur (for SDK to get loaded)
45
+ window.rudderSnippetLoadedCallback = function () {
46
+ trackRudderStackPage();
47
+ }
48
+ }
25
49
  }
50
+ } else {
51
+ trackRudderStackPage();
26
52
  }
27
53
  };
package/gatsby-ssr.js CHANGED
@@ -4,65 +4,129 @@ var _react = require("react");
4
4
 
5
5
  var _react2 = _interopRequireDefault(_react);
6
6
 
7
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
7
+ function _interopRequireDefault(obj) {
8
+ return obj && obj.__esModule ? obj : { default: obj };
9
+ }
8
10
 
9
11
  exports.onRenderBody = function (_ref, pluginOptions) {
10
12
  var setHeadComponents = _ref.setHeadComponents;
11
13
  var trackPage = pluginOptions.trackPage,
12
- prodKey = pluginOptions.prodKey,
13
- devKey = pluginOptions.devKey,
14
- _pluginOptions$dataPl = pluginOptions.dataPlaneUrl,
15
- dataPlaneUrl = _pluginOptions$dataPl === undefined ? "https://hosted.rudderlabs.com" : _pluginOptions$dataPl,
16
- controlPlaneUrl = pluginOptions.controlPlaneUrl,
17
- delayLoad = pluginOptions.delayLoad,
18
- delayLoadTime = pluginOptions.delayLoadTime,
19
- manualLoad = pluginOptions.manualLoad,
20
- loadType = pluginOptions.loadType,
21
- useNewSDK = pluginOptions.useNewSDK;
22
-
14
+ prodKey = pluginOptions.prodKey,
15
+ devKey = pluginOptions.devKey,
16
+ _pluginOptions$dataPl = pluginOptions.dataPlaneUrl,
17
+ dataPlaneUrl =
18
+ _pluginOptions$dataPl === undefined
19
+ ? "https://hosted.rudderlabs.com"
20
+ : _pluginOptions$dataPl,
21
+ controlPlaneUrl = pluginOptions.controlPlaneUrl,
22
+ delayLoad = pluginOptions.delayLoad,
23
+ delayLoadTime = pluginOptions.delayLoadTime,
24
+ manualLoad = pluginOptions.manualLoad,
25
+ loadType = pluginOptions.loadType,
26
+ useNewSDK = pluginOptions.useNewSDK,
27
+ loadOptions = pluginOptions.loadOptions,
28
+ sdkURL = pluginOptions.sdkURL;
23
29
 
24
30
  var sdkSrc = "https://cdn.rudderlabs.com/v1/rudder-analytics.min.js";
25
- if (useNewSDK) {
31
+ if (sdkURL) sdkSrc = sdkURL;
32
+ else if (useNewSDK) {
26
33
  sdkSrc = "https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js";
27
34
  }
28
35
 
29
- if (!prodKey || prodKey.length < 10) console.error("Your Rudderstack prodKey must be at least 10 char in length.");
36
+ if (!prodKey || prodKey.length < 10)
37
+ console.error(
38
+ "Your RudderStack prodKey must be at least 10 char in length."
39
+ );
30
40
 
31
- if (devKey && devKey.length < 10) console.error("If present, your Rudderstack devKey must be at least 10 char in length.");
41
+ if (devKey && devKey.length < 10)
42
+ console.error(
43
+ "If present, your RudderStack devKey must be at least 10 char in length."
44
+ );
32
45
 
33
46
  var writeKey = process.env.NODE_ENV === "production" ? prodKey : devKey;
34
47
 
35
- var loadConfig = controlPlaneUrl ? "'" + writeKey + "', '" + dataPlaneUrl + "', {configUrl: '" + controlPlaneUrl + "'}" : "'" + writeKey + "', '" + dataPlaneUrl + "'";
36
-
37
-
38
- var snippet = "rudderanalytics=window.rudderanalytics=[];for(var methods=[\"load\",\"page\",\"track\",\"identify\",\"alias\",\"group\",\"ready\",\"reset\",\"getAnonymousId\",\"setAnonymousId\"],i=0;i<methods.length;i++){var method=methods[i];rudderanalytics[method]=function(a){return function(){rudderanalytics.push([a].concat(Array.prototype.slice.call(arguments)))}}(method)}\n " + (delayLoad || manualLoad ? "" : "rudderanalytics.load(" + loadConfig + ")") + ";\n";
48
+ var loadConfig = "'" + writeKey + "', '" + dataPlaneUrl + "'";
39
49
 
40
- var delayedLoader = "\n window.rudderSnippetLoaded = false;\n window.rudderSnippetLoading = false;\n window.rudderSnippetLoader = function (callback) {\n if (!window.rudderSnippetLoaded && !window.rudderSnippetLoading) {\n window.rudderSnippetLoading = true;\n function loader() {\n window.rudderanalytics.load(" + loadConfig + ");\n window.rudderSnippetLoading = false;\n window.rudderSnippetLoaded = true;\n window.rudderanalytics.page(document.title);\n if(callback) {callback()}\n };\n setTimeout(\n function () {\n \"requestIdleCallback\" in window\n ? requestIdleCallback(function () {loader()})\n : loader();\n },\n " + delayLoadTime + " || 1000\n );\n }\n }\n window.addEventListener('scroll',function () {window.rudderSnippetLoader()}, { once: true });\n ";
50
+ if (loadOptions) {
51
+ // Override config URL if provided separately
52
+ loadOptions.configUrl = controlPlaneUrl || loadOptions.configUrl;
53
+ loadConfig += ", " + JSON.stringify(loadOptions);
54
+ }
41
55
 
42
- var snippetToUse = "\n " + (delayLoad && !manualLoad ? delayedLoader : "") + "\n " + snippet + "\n ";
56
+ var snippet =
57
+ 'rudderanalytics=window.rudderanalytics=[];for(var methods=["load","page","track","identify","alias","group","ready","reset","getAnonymousId","setAnonymousId"],i=0;i<methods.length;i++){var method=methods[i];rudderanalytics[method]=function(a){return function(){rudderanalytics.push([a].concat(Array.prototype.slice.call(arguments)))}}(method)}\n ' +
58
+ (delayLoad || manualLoad
59
+ ? ""
60
+ : "rudderanalytics.load(" + loadConfig + ")") +
61
+ ";\n";
62
+
63
+ const delayedLoader = `
64
+ window.rudderSnippetLoaded = false;
65
+ window.rudderSnippetLoading = false;
66
+ window.rudderSnippetLoadedCallback = undefined;
67
+ window.rudderSnippetLoader = function (callback) {
68
+ if (!window.rudderSnippetLoaded && !window.rudderSnippetLoading) {
69
+ window.rudderSnippetLoading = true;
70
+ function loader() {
71
+ window.rudderanalytics.load(${loadConfig});
72
+ window.rudderSnippetLoading = false;
73
+ window.rudderSnippetLoaded = true;
74
+ if (callback) { callback(); }
75
+ if (window.rudderSnippetLoadedCallback) {
76
+ window.rudderSnippetLoadedCallback();
77
+ window.rudderSnippetLoadedCallback = undefined;
78
+ }
79
+ };
80
+
81
+ "requestIdleCallback" in window
82
+ ? requestIdleCallback(function () { loader(); })
83
+ : loader();
84
+ }
85
+ }
86
+ window.addEventListener('scroll',function () {window.rudderSnippetLoader()}, { once: true });
87
+ setTimeout(
88
+ function () {
89
+ "requestIdleCallback" in window
90
+ ? requestIdleCallback(function () { window.rudderSnippetLoader(); })
91
+ : window.rudderSnippetLoader();
92
+ },
93
+ ${delayLoadTime} || 1000
94
+ );
95
+ `;
96
+
97
+ var snippetToUse =
98
+ "\n " +
99
+ (delayLoad && !manualLoad ? delayedLoader : "") +
100
+ "\n " +
101
+ snippet +
102
+ "\n ";
43
103
 
44
104
  if (writeKey) {
45
- var tags = [_react2.default.createElement("script", {
46
- key: "plugin-rudderstack",
47
- dangerouslySetInnerHTML: { __html: snippetToUse }
48
- })];
105
+ var tags = [
106
+ _react2.default.createElement("script", {
107
+ key: "plugin-rudderstack",
108
+ dangerouslySetInnerHTML: { __html: snippetToUse },
109
+ }),
110
+ ];
49
111
 
50
112
  var tag = void 0;
51
113
 
52
- if (loadType == 'async') {
53
- tag = _react2.default.createElement("script", { async: true,
114
+ if (loadType == "async") {
115
+ tag = _react2.default.createElement("script", {
116
+ async: true,
54
117
  key: "rudderstack-cdn",
55
- src: sdkSrc
118
+ src: sdkSrc,
56
119
  });
57
- } else if (loadType == 'defer') {
58
- tag = _react2.default.createElement("script", { defer: true,
120
+ } else if (loadType == "defer") {
121
+ tag = _react2.default.createElement("script", {
122
+ defer: true,
59
123
  key: "rudderstack-cdn",
60
- src: sdkSrc
124
+ src: sdkSrc,
61
125
  });
62
126
  } else {
63
127
  tag = _react2.default.createElement("script", {
64
128
  key: "rudderstack-cdn",
65
- src: sdkSrc
129
+ src: sdkSrc,
66
130
  });
67
131
  }
68
132
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatsby-plugin-rudderstack",
3
- "version": "1.0.4",
4
- "description": "Event tracking with Rudderstack in your Gatsby frontend.",
3
+ "version": "1.1.1",
4
+ "description": "Event tracking with RudderStack in your Gatsby frontend.",
5
5
  "main": "gatsby-ssr.js",
6
6
  "scripts": {
7
7
  "build": "babel src --out-dir . --ignore __tests__",
@@ -18,7 +18,7 @@
18
18
  "gatsby",
19
19
  "gatsby-plugin"
20
20
  ],
21
- "author": "Rudderstack",
21
+ "author": "RudderStack",
22
22
  "license": "AGPL-3.0-only",
23
23
  "bugs": {
24
24
  "url": "https://github.com/rudderlabs/gatsby-plugin-rudderstack/issues"