forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only load Segment's script if
gatherUserStats
is true (streamlit#1723)
* Dynamically loading Segment-io script * Added Segment.ts * Removed unused function * fixed linting issues * fixed linting issues with Segment.ts * fixed frontend test cases * Changes based on comments * Corrected spellings and removed package-lock.json * Update .gitignore removed unnecessary space * removed window check
- Loading branch information
1 parent
b2c3bec
commit 7cd06d5
Showing
4 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
declare global { | ||
interface Window { | ||
analytics: any | ||
} | ||
} | ||
/** @function initializeSegment | ||
* Loads the global analytics service provided segment.io | ||
* @see {@link https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/quickstart/#} | ||
* @version 4.1.0 | ||
*/ | ||
export const initializeSegment = (): void => { | ||
// Create a queue, but don't obliterate an existing one! | ||
const analytics = (window.analytics = window.analytics || []) | ||
|
||
// If the real analytics.js is already on the page return. | ||
if (analytics.initialize) return | ||
|
||
// If the snippet was invoked already show an error. | ||
if (analytics.invoked) { | ||
if (window.console && console.error) { | ||
console.error("Segment snippet included twice.") | ||
} | ||
return | ||
} | ||
|
||
// Invoked flag, to make sure the snippet | ||
// is never invoked twice. | ||
analytics.invoked = true | ||
|
||
// A list of the methods in Analytics.js to stub. | ||
analytics.methods = [ | ||
"trackSubmit", | ||
"trackClick", | ||
"trackLink", | ||
"trackForm", | ||
"pageview", | ||
"identify", | ||
"reset", | ||
"group", | ||
"track", | ||
"ready", | ||
"alias", | ||
"debug", | ||
"page", | ||
"once", | ||
"off", | ||
"on", | ||
"addSourceMiddleware", | ||
"addIntegrationMiddleware", | ||
"setAnonymousId", | ||
"addDestinationMiddleware", | ||
] | ||
|
||
// Define a factory to create stubs. These are placeholders | ||
// for methods in Analytics.js so that you never have to wait | ||
// for it to load to actually record data. The `method` is | ||
// stored as the first argument, so we can replay the data. | ||
analytics.factory = function(method: any) { | ||
return function(...args: any[]) { | ||
const _args = Array.prototype.slice.call(args) | ||
_args.unshift(method) | ||
analytics.push(_args) | ||
return analytics | ||
} | ||
} | ||
|
||
// For each of our methods, generate a queueing stub. | ||
for (let i = 0; i < analytics.methods.length; i++) { | ||
const key = analytics.methods[i] | ||
analytics[key] = analytics.factory(key) | ||
} | ||
|
||
// Define a method to load Analytics.js from our CDN, | ||
// and that will be sure to only ever load it once. | ||
analytics.load = function(key: string, options: any) { | ||
// Create an async script element based on your key. | ||
const script = document.createElement("script") | ||
script.type = "text/javascript" | ||
script.async = true | ||
script.src = | ||
"https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js" | ||
|
||
// Insert our script next to the first script element. | ||
const first = document.getElementsByTagName("script")[0] | ||
if (first && first.parentNode) first.parentNode.insertBefore(script, first) | ||
analytics._loadOptions = options | ||
} | ||
|
||
// Add a version to keep track of what's in the wild. | ||
analytics.SNIPPET_VERSION = "4.1.0" | ||
|
||
// Load Analytics.js with your key, which will automatically | ||
// load the tools you've enabled for your account. Boosh! | ||
|
||
analytics.load("iCkMy7ymtJ9qYzQRXkQpnAJEq7D4NyMU") | ||
} |