Skip to content

Commit 3c81fd7

Browse files
authored
feat: use js sdk v3 (#30)
* feat: use js sdk v3 * chore: update code owners * chore: fix readme * chore: dummy commit to trigger workflows * refactor: use const
1 parent 9e67968 commit 3c81fd7

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

CODEOWNERS

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* @bardisg
2-
* @MoumitaM
1+
* @rudderlabs/js-sdk

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ plugins: [
120120
// 'defer' will load the SDK as <script defer></script>
121121
loadType: 'default',
122122

123-
// Whether to use the snippet for JS SDK v2.x
124-
// Useful for only loading the previous major version of the SDK if you do not want to migrate to v3 yet.
125-
useLegacySDK: false,
126-
127123
// Options to the `load` API
128124
// Note: The above `controlPlaneUrl` overrides the `configUrl` field in this object
129125
// You can find all the available load options here,

src/gatsby-ssr.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
99
manualLoad,
1010
loadType,
1111
sdkURL = 'https://cdn.rudderlabs.com/v3',
12-
loadOptions = {},
13-
useLegacySDK,
12+
loadOptions = {}
1413
} = pluginOptions;
1514

1615
// ensures RudderStack production write key is present
@@ -32,29 +31,20 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
3231

3332
const loadConfig = `'${writeKey}', '${dataPlaneUrl}', ${JSON.stringify(finalLoadOptions)}`;
3433

35-
let snippet = `window.RudderSnippetVersion="3.0.0-beta.14";var sdkBaseUrl="${sdkURL}";var sdkName="rsa.min.js";var asyncScript=true;window.rudderAnalyticsBuildType="legacy",window.rudderanalytics=[];
36-
var e=["setDefaultInstanceKey","load","ready","page","track","identify","alias","group","reset","setAnonymousId","startSession","endSession","consent"];
37-
for(var n=0;n<e.length;n++){var t=e[n];window.rudderanalytics[t]=function(e){return function(){window.rudderanalytics.push([e].concat(Array.prototype.slice.call(arguments)))}}(t)}
38-
try{new Function('return import("")'),window.rudderAnalyticsBuildType="modern"}catch(a){}
39-
if(window.rudderAnalyticsMount=function(){"undefined"==typeof globalThis&&(Object.defineProperty(Object.prototype,"__globalThis_magic__",{get:function get(){return this},configurable:true}),__globalThis_magic__.globalThis=__globalThis_magic__,delete Object.prototype.__globalThis_magic__);
40-
var e=document.createElement("script");e.src="".concat(sdkBaseUrl,"/").concat(window.rudderAnalyticsBuildType,"/").concat(sdkName),e.async=asyncScript,document.head?document.head.appendChild(e):document.body.appendChild(e)},"undefined"==typeof Promise||"undefined"==typeof globalThis){
41-
var d=document.createElement("script");d.src="https://polyfill.io/v3/polyfill.min.js?features=Symbol%2CPromise&callback=rudderAnalyticsMount",d.async=asyncScript,document.head?document.head.appendChild(d):document.body.appendChild(d)}else{window.rudderAnalyticsMount()}
42-
`;
43-
44-
if (useLegacySDK) {
45-
let scriptTagStr = `var s = document.createElement("script");
46-
s.type = "text/javascript";
47-
s.src = "${sdkURL}";`;
48-
if (loadType === 'async') {
49-
scriptTagStr += 's.async = true;';
50-
} else if (loadType === 'defer') {
51-
scriptTagStr += 's.defer = true;';
52-
}
53-
scriptTagStr += 'document.head.appendChild(s);';
54-
55-
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)}
56-
${scriptTagStr}`;
57-
}
34+
const snippet = `!function(){"use strict";window.RudderSnippetVersion="3.0.3";var sdkBaseUrl=${sdkURL};var sdkName="rsa.min.js";var asyncScript=${loadType === 'async'};var deferScript=${loadType === 'defer'};window.rudderAnalyticsBuildType="legacy",window.rudderanalytics=[]
35+
;var e=["setDefaultInstanceKey","load","ready","page","track","identify","alias","group","reset","setAnonymousId","startSession","endSession","consent"]
36+
;for(var n=0;n<e.length;n++){var t=e[n];window.rudderanalytics[t]=function(e){return function(){
37+
window.rudderanalytics.push([e].concat(Array.prototype.slice.call(arguments)))}}(t)}try{
38+
new Function('return import("")'),window.rudderAnalyticsBuildType="modern"}catch(a){}
39+
if(window.rudderAnalyticsMount=function(){
40+
"undefined"==typeof globalThis&&(Object.defineProperty(Object.prototype,"__globalThis_magic__",{get:function get(){
41+
return this},configurable:true}),__globalThis_magic__.globalThis=__globalThis_magic__,
42+
delete Object.prototype.__globalThis_magic__);var e=document.createElement("script")
43+
;e.src="".concat(sdkBaseUrl,"/").concat(window.rudderAnalyticsBuildType,"/").concat(sdkName);if(asyncScript){e.async=true};if(deferScript){e.defer=true};document.head?document.head.appendChild(e):document.body.appendChild(e)
44+
},"undefined"==typeof Promise||"undefined"==typeof globalThis){var d=document.createElement("script")
45+
;d.src="https://polyfill-fastly.io/v3/polyfill.min.js?version=3.111.0&features=Symbol%2CPromise&callback=rudderAnalyticsMount",
46+
d.async=asyncScript,document.head?document.head.appendChild(d):document.body.appendChild(d)}else{
47+
window.rudderAnalyticsMount()}}();`;
5848

5949
const instantLoader = `${snippet}${
6050
manualLoad ? `` : `window.rudderanalytics.load(${loadConfig})`

0 commit comments

Comments
 (0)