gatsby-plugin-rudderstack 1.0.1 → 1.0.5
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 +20 -16
- package/gatsby-browser.js +39 -12
- package/gatsby-ssr.js +60 -12
- package/package.json +1 -1
- package/CHANGELOG.md +0 -0
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
|
|
6
|
+
# The RudderStack Plugin for Gatsby.
|
|
7
7
|
|
|
8
|
-
This is a way for you to quickly and easily get
|
|
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
|
|
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 (
|
|
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
|
-
//
|
|
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
|
-
//
|
|
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
|
|
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
|
|
73
|
-
// or user scroll, whichever comes first. This
|
|
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
|
-
//
|
|
78
|
-
// a full page refresh, server-side routing does, thereby preventing
|
|
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,7 +82,8 @@ 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
|
|
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
88
|
delayLoadTime: 1000
|
|
89
89
|
|
|
@@ -95,9 +95,11 @@ plugins: [
|
|
|
95
95
|
// *Another use case is if you want to add callbacks to the methods at load time.
|
|
96
96
|
manualLoad: false
|
|
97
97
|
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
|
|
98
|
+
// string ('async' or 'defer'); whether to load the RudderStack SDK async or defer. Anything else
|
|
99
|
+
// will load normally.
|
|
100
|
+
// 'async' will load the SDK as <script async></script>
|
|
101
|
+
// 'defer' will load the SDK as <script defer></script>
|
|
102
|
+
loadType: 'default'
|
|
101
103
|
}
|
|
102
104
|
}
|
|
103
105
|
];
|
|
@@ -189,6 +191,8 @@ exports.onRouteUpdate = () => {
|
|
|
189
191
|
};
|
|
190
192
|
```
|
|
191
193
|
|
|
194
|
+
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.
|
|
195
|
+
|
|
192
196
|
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!
|
|
193
197
|
|
|
194
198
|
## [](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,21 +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
|
|
10
|
-
|
|
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
|
-
|
|
13
|
-
if (trackPage) {
|
|
17
|
+
window.setTimeout(function () {
|
|
14
18
|
window.rudderanalytics && window.rudderanalytics.page(document.title);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
19
|
+
}, delay);
|
|
20
|
+
}
|
|
17
21
|
}
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
};
|
|
33
|
+
} else {
|
|
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
|
+
}
|
|
49
|
+
}
|
|
23
50
|
} else {
|
|
24
|
-
|
|
51
|
+
trackRudderStackPage();
|
|
25
52
|
}
|
|
26
|
-
};
|
|
53
|
+
};
|
package/gatsby-ssr.js
CHANGED
|
@@ -17,7 +17,14 @@ exports.onRenderBody = function (_ref, pluginOptions) {
|
|
|
17
17
|
delayLoad = pluginOptions.delayLoad,
|
|
18
18
|
delayLoadTime = pluginOptions.delayLoadTime,
|
|
19
19
|
manualLoad = pluginOptions.manualLoad,
|
|
20
|
-
|
|
20
|
+
loadType = pluginOptions.loadType,
|
|
21
|
+
useNewSDK = pluginOptions.useNewSDK;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
var sdkSrc = "https://cdn.rudderlabs.com/v1/rudder-analytics.min.js";
|
|
25
|
+
if (useNewSDK) {
|
|
26
|
+
sdkSrc = "https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js";
|
|
27
|
+
}
|
|
21
28
|
|
|
22
29
|
if (!prodKey || prodKey.length < 10) console.error("Your Rudderstack prodKey must be at least 10 char in length.");
|
|
23
30
|
|
|
@@ -25,14 +32,44 @@ exports.onRenderBody = function (_ref, pluginOptions) {
|
|
|
25
32
|
|
|
26
33
|
var writeKey = process.env.NODE_ENV === "production" ? prodKey : devKey;
|
|
27
34
|
|
|
28
|
-
var includeTrackPage = !trackPage ? "" : "rudderanalytics.page();";
|
|
29
|
-
|
|
30
35
|
var loadConfig = controlPlaneUrl ? "'" + writeKey + "', '" + dataPlaneUrl + "', {configUrl: '" + controlPlaneUrl + "'}" : "'" + writeKey + "', '" + dataPlaneUrl + "'";
|
|
31
36
|
|
|
32
37
|
|
|
33
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";
|
|
34
39
|
|
|
35
|
-
|
|
40
|
+
const delayedLoader = `
|
|
41
|
+
window.rudderSnippetLoaded = false;
|
|
42
|
+
window.rudderSnippetLoading = false;
|
|
43
|
+
window.rudderSnippetLoadedCallback = undefined;
|
|
44
|
+
window.rudderSnippetLoader = function (callback) {
|
|
45
|
+
if (!window.rudderSnippetLoaded && !window.rudderSnippetLoading) {
|
|
46
|
+
window.rudderSnippetLoading = true;
|
|
47
|
+
function loader() {
|
|
48
|
+
window.rudderanalytics.load(${loadConfig});
|
|
49
|
+
window.rudderSnippetLoading = false;
|
|
50
|
+
window.rudderSnippetLoaded = true;
|
|
51
|
+
if (callback) { callback(); }
|
|
52
|
+
if (window.rudderSnippetLoadedCallback) {
|
|
53
|
+
window.rudderSnippetLoadedCallback();
|
|
54
|
+
window.rudderSnippetLoadedCallback = undefined;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
"requestIdleCallback" in window
|
|
59
|
+
? requestIdleCallback(function () { loader(); })
|
|
60
|
+
: loader();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
window.addEventListener('scroll',function () {window.rudderSnippetLoader()}, { once: true });
|
|
64
|
+
setTimeout(
|
|
65
|
+
function () {
|
|
66
|
+
"requestIdleCallback" in window
|
|
67
|
+
? requestIdleCallback(function () { window.rudderSnippetLoader(); })
|
|
68
|
+
: window.rudderSnippetLoader();
|
|
69
|
+
},
|
|
70
|
+
${delayLoadTime} || 1000
|
|
71
|
+
);
|
|
72
|
+
`;
|
|
36
73
|
|
|
37
74
|
var snippetToUse = "\n " + (delayLoad && !manualLoad ? delayedLoader : "") + "\n " + snippet + "\n ";
|
|
38
75
|
|
|
@@ -42,15 +79,26 @@ exports.onRenderBody = function (_ref, pluginOptions) {
|
|
|
42
79
|
dangerouslySetInnerHTML: { __html: snippetToUse }
|
|
43
80
|
})];
|
|
44
81
|
|
|
45
|
-
var tag =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
82
|
+
var tag = void 0;
|
|
83
|
+
|
|
84
|
+
if (loadType == 'async') {
|
|
85
|
+
tag = _react2.default.createElement("script", { async: true,
|
|
86
|
+
key: "rudderstack-cdn",
|
|
87
|
+
src: sdkSrc
|
|
88
|
+
});
|
|
89
|
+
} else if (loadType == 'defer') {
|
|
90
|
+
tag = _react2.default.createElement("script", { defer: true,
|
|
91
|
+
key: "rudderstack-cdn",
|
|
92
|
+
src: sdkSrc
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
tag = _react2.default.createElement("script", {
|
|
96
|
+
key: "rudderstack-cdn",
|
|
97
|
+
src: sdkSrc
|
|
98
|
+
});
|
|
99
|
+
}
|
|
52
100
|
|
|
53
101
|
tags.push(tag);
|
|
54
102
|
setHeadComponents(tags);
|
|
55
103
|
}
|
|
56
|
-
};
|
|
104
|
+
};
|
package/package.json
CHANGED
package/CHANGELOG.md
DELETED
|
File without changes
|