-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-63] [New feature] Add segment to Opik Cloud (#201)
- Loading branch information
1 parent
d979e15
commit 4fd6400
Showing
10 changed files
with
146 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
window.environmentVariablesOverwrite = {}; |
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,16 @@ | ||
import initSnippet from "./snippet"; | ||
|
||
export const initAnalytics = (writeKey?: string) => { | ||
if (writeKey) { | ||
initSnippet(writeKey); | ||
} | ||
}; | ||
|
||
export const trackEvent = ( | ||
name: string, | ||
properties: Record<string, unknown>, | ||
) => { | ||
if (window.analytics) { | ||
window.analytics.track(name, properties); | ||
} | ||
}; |
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,83 @@ | ||
export default function initSnippet(writeKey) { | ||
var i = "analytics", | ||
analytics = (window[i] = window[i] || []); | ||
if (!analytics.initialize) | ||
if (analytics.invoked) | ||
window.console && | ||
console.error && | ||
console.error("Segment snippet included twice."); | ||
else { | ||
analytics.invoked = !0; | ||
analytics.methods = [ | ||
"trackSubmit", | ||
"trackClick", | ||
"trackLink", | ||
"trackForm", | ||
"pageview", | ||
"identify", | ||
"reset", | ||
"group", | ||
"track", | ||
"ready", | ||
"alias", | ||
"debug", | ||
"page", | ||
"screen", | ||
"once", | ||
"off", | ||
"on", | ||
"addSourceMiddleware", | ||
"addIntegrationMiddleware", | ||
"setAnonymousId", | ||
"addDestinationMiddleware", | ||
"register", | ||
]; | ||
analytics.factory = function (e) { | ||
return function () { | ||
if (window[i].initialized) | ||
return window[i][e].apply(window[i], arguments); | ||
var n = Array.prototype.slice.call(arguments); | ||
if ( | ||
["track", "screen", "alias", "group", "page", "identify"].indexOf( | ||
e, | ||
) > -1 | ||
) { | ||
var c = document.querySelector("link[rel='canonical']"); | ||
n.push({ | ||
__t: "bpc", | ||
c: (c && c.getAttribute("href")) || void 0, | ||
p: location.pathname, | ||
u: location.href, | ||
s: location.search, | ||
t: document.title, | ||
r: document.referrer, | ||
}); | ||
} | ||
n.unshift(e); | ||
analytics.push(n); | ||
return analytics; | ||
}; | ||
}; | ||
for (var n = 0; n < analytics.methods.length; n++) { | ||
var key = analytics.methods[n]; | ||
analytics[key] = analytics.factory(key); | ||
} | ||
analytics.load = function (key, n) { | ||
var t = document.createElement("script"); | ||
t.type = "text/javascript"; | ||
t.async = !0; | ||
t.setAttribute("data-global-segment-analytics-key", i); | ||
t.src = | ||
"https://cdn.segment.com/analytics.js/v1/" + | ||
key + | ||
"/analytics.min.js"; | ||
var r = document.getElementsByTagName("script")[0]; | ||
r.parentNode.insertBefore(t, r); | ||
analytics._loadOptions = n; | ||
}; | ||
analytics._writeKey = writeKey; | ||
analytics.SNIPPET_VERSION = "5.2.0"; | ||
analytics.load(writeKey); | ||
analytics.page(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/opik-frontend/src/plugins/comet/analytics/useSegment.ts
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,11 @@ | ||
import { useEffect } from "react"; | ||
|
||
const useSegment = (username?: string) => { | ||
useEffect(() => { | ||
if (window.analytics && username) { | ||
window.analytics.identify(username); | ||
} | ||
}, [username]); | ||
}; | ||
|
||
export default useSegment; |
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,16 @@ | ||
/// <reference types="@types/segment-analytics" /> | ||
|
||
import { initAnalytics } from "./analytics"; | ||
|
||
type EnvironmentVariablesOverwrite = { | ||
OPIK_SEGMENT_ID?: string; | ||
}; | ||
|
||
declare global { | ||
interface Window { | ||
analytics: SegmentAnalytics.AnalyticsJS; | ||
environmentVariablesOverwrite: EnvironmentVariablesOverwrite; | ||
} | ||
} | ||
|
||
initAnalytics(window.environmentVariablesOverwrite.OPIK_SEGMENT_ID); |
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