From ce7311f4a36f0bf3da06d54fc7f56eda419c5938 Mon Sep 17 00:00:00 2001 From: prabrishac Date: Fri, 29 Jan 2021 22:15:52 +0530 Subject: [PATCH 1/4] initial commit for Posthog --- integrations/Posthog/browser.js | 123 ++++++++++++++ integrations/Posthog/index.js | 3 + integrations/Posthog/test/test_amplitude.html | 158 ++++++++++++++++++ integrations/index.js | 4 +- 4 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 integrations/Posthog/browser.js create mode 100644 integrations/Posthog/index.js create mode 100644 integrations/Posthog/test/test_amplitude.html diff --git a/integrations/Posthog/browser.js b/integrations/Posthog/browser.js new file mode 100644 index 0000000000..312e072cff --- /dev/null +++ b/integrations/Posthog/browser.js @@ -0,0 +1,123 @@ +/* eslint-disable class-methods-use-this */ +import logger from "../../utils/logUtil"; + +class Posthog { + constructor(config, analytics) { + this.name = "POSTHOG"; + this.analytics = analytics; + this.teamApiKey = config.teamApiKey; + this.yourInstance = config.yourInstance || "https://app.posthog.com"; + this.autocapture = config.autocapture; + this.capturePageView = config.capturePageView; + this.disableSessionRecording = config.disableSessionRecording; + this.propertyBlackList = []; + this.xhrHeaders = {}; + this.superProperties = {}; + + if (config.superProperties && config.superProperties.length > 0) { + config.superProperties.forEach(property => { + if(property && property.key && property.value && property.key.trim() != "" && property.value.trim() != ""){ + this.superProperties[property.key] = property.value; + } + }); + } + if (config.xhrHeaders && config.xhrHeaders.length > 0) { + config.xhrHeaders.forEach(header => { + if(header && header.key && header.value && header.key.trim() != "" && header.value.trim() != ""){ + this.xhrHeaders[header.key] = header.value; + } + }); + } + if (config.propertyBlackList && config.propertyBlackList.length > 0) { + config.propertyBlackList.forEach(element => { + if(element && element.property && element.property.trim() != ""){ + this.propertyBlackList.push(element.property); + } + }); + } + } + + init() { + ! function (t, e) { + var o, n, p, r; + e.__SV || (window.posthog = e, e._i = [], e.init = function (i, s, a) { + function g(t, e) { + var o = e.split("."); + 2 == o.length && (t = t[o[0]], e = o[1]), t[e] = function () { + t.push([e].concat(Array.prototype.slice.call(arguments, 0))) + } + }(p = t.createElement("script")).type = "text/javascript", p.async = !0, p.src = s.api_host + "/static/array.js", (r = t.getElementsByTagName("script")[0]).parentNode.insertBefore(p, r); + var u = e; + for (void 0 !== a ? u = e[a] = [] : a = "posthog", u.people = u.people || [], u.toString = function (t) { + var e = "posthog"; + return "posthog" !== a && (e += "." + a), t || (e += " (stub)"), e + }, u.people.toString = function () { + return u.toString(1) + ".people (stub)" + }, o = "capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "), n = 0; n < o.length; n++) g(u, o[n]); + e._i.push([i, s, a]) + }, e.__SV = 1) + }(document, window.posthog || []); + + const configObject = {api_host: this.yourInstance, + autocapture: this.autocapture, + capture_pageview: this.capturePageView, + disable_session_recording: this.disableSessionRecording, + property_blacklist: this.propertyBlackList + }; + if(this.xhrHeaders && Object.keys(this.xhrHeaders).length > 0){ + configObject.xhr_headers = {};//this.xhrHeaders; + } + + + posthog.init(this.teamApiKey, configObject); + if(this.superProperties && Object.keys(this.superProperties).length > 0){ + posthog.register(this.superProperties); + } + } + + identify(rudderElement) { + logger.debug("in Posthog identify"); + + // rudderElement.message.context will always be present as part of identify event payload. + const { traits } = rudderElement.message.context; + const { userId } = rudderElement.message; + + if (userId) { + window.posthog.identify(userId); + } + + if (traits) { + window.posthog.people.set(traits); + } + } + + track(rudderElement) { + logger.debug("in Posthog track"); + + const { event, properties } = rudderElement.message; + + window.posthog.capture(event, properties); + } + + /** + * + * + * @memberof Posthog + */ + page(rudderElement) { + logger.debug("in Posthog page"); + + window.posthog.capture('$pageview'); + } + + isLoaded() { + logger.debug("in Posthog isLoaded"); + return !!(window.posthog && window.posthog.__loaded); + } + + isReady() { + return !!(window.posthog && window.posthog.__loaded); + } +} + +export default Posthog; diff --git a/integrations/Posthog/index.js b/integrations/Posthog/index.js new file mode 100644 index 0000000000..4939def21d --- /dev/null +++ b/integrations/Posthog/index.js @@ -0,0 +1,3 @@ +import Posthog from "./browser"; + +export default Posthog; diff --git a/integrations/Posthog/test/test_amplitude.html b/integrations/Posthog/test/test_amplitude.html new file mode 100644 index 0000000000..d9cf029c60 --- /dev/null +++ b/integrations/Posthog/test/test_amplitude.html @@ -0,0 +1,158 @@ + + + + + + + +

Relax while I finish my testing process....

+ + \ No newline at end of file diff --git a/integrations/index.js b/integrations/index.js index f4fa918899..7c7bb12114 100644 --- a/integrations/index.js +++ b/integrations/index.js @@ -23,6 +23,7 @@ import * as Amplitude from "./Amplitude"; import * as Pendo from "./Pendo"; import * as Lytics from "./Lytics"; import * as Appcues from "./Appcues"; +import * as Posthog from "./Posthog"; // the key names should match the destination.name value to keep partity everywhere // (config-plan name, native destination.name , exported integration name(this one below)) @@ -52,7 +53,8 @@ const integrations = { AM: Amplitude.default, PENDO: Pendo.default, LYTICS: Lytics.default, - APPCUES: Appcues.default + APPCUES: Appcues.default, + POSTHOG: Posthog.default }; export { integrations }; From 0d8fda659de5f2c78ff2260f11c64a04ffd920a3 Mon Sep 17 00:00:00 2001 From: prabrishac Date: Mon, 1 Feb 2021 12:01:14 +0530 Subject: [PATCH 2/4] added integration name mapping --- integrations/client_server_name.js | 1 + integrations/integration_cname.js | 3 +++ 2 files changed, 4 insertions(+) diff --git a/integrations/client_server_name.js b/integrations/client_server_name.js index 173f1b2478..3ff84b09e4 100644 --- a/integrations/client_server_name.js +++ b/integrations/client_server_name.js @@ -26,6 +26,7 @@ const clientToServerNames = { PENDO: "Pendo", LYTICS: "Lytics", APPCUES: "Appcues", + POSTHOG: "PostHog" }; export { clientToServerNames }; diff --git a/integrations/integration_cname.js b/integrations/integration_cname.js index 45766d81d9..6107153587 100644 --- a/integrations/integration_cname.js +++ b/integrations/integration_cname.js @@ -56,6 +56,9 @@ const commonNames = { LYTICS: "Lytics", Appcues: "APPCUES", APPCUES: "APPCUES", + POSTHOG: "POSTHOG", + PostHog: "POSTHOG", + Posthog: "POSTHOG" }; export { commonNames }; From ca55bcbc1b97ff4004d8186109cfdfedeb7b05ec Mon Sep 17 00:00:00 2001 From: prabrishac Date: Thu, 4 Feb 2021 14:43:51 +0530 Subject: [PATCH 3/4] added and rearranged config --- integrations/Posthog/browser.js | 62 +++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/integrations/Posthog/browser.js b/integrations/Posthog/browser.js index 312e072cff..25e2aee310 100644 --- a/integrations/Posthog/browser.js +++ b/integrations/Posthog/browser.js @@ -7,20 +7,13 @@ class Posthog { this.analytics = analytics; this.teamApiKey = config.teamApiKey; this.yourInstance = config.yourInstance || "https://app.posthog.com"; - this.autocapture = config.autocapture; - this.capturePageView = config.capturePageView; - this.disableSessionRecording = config.disableSessionRecording; + this.autocapture = config.autocapture || false; + this.capturePageView = config.capturePageView || false; + this.disableSessionRecording = config.disableSessionRecording || false; + this.disableCookie = config.disableCookie || false; this.propertyBlackList = []; this.xhrHeaders = {}; - this.superProperties = {}; - if (config.superProperties && config.superProperties.length > 0) { - config.superProperties.forEach(property => { - if(property && property.key && property.value && property.key.trim() != "" && property.value.trim() != ""){ - this.superProperties[property.key] = property.value; - } - }); - } if (config.xhrHeaders && config.xhrHeaders.length > 0) { config.xhrHeaders.forEach(header => { if(header && header.key && header.value && header.key.trim() != "" && header.value.trim() != ""){ @@ -62,17 +55,40 @@ class Posthog { autocapture: this.autocapture, capture_pageview: this.capturePageView, disable_session_recording: this.disableSessionRecording, - property_blacklist: this.propertyBlackList + property_blacklist: this.propertyBlackList, + disable_cookie: this.disableCookie }; if(this.xhrHeaders && Object.keys(this.xhrHeaders).length > 0){ - configObject.xhr_headers = {};//this.xhrHeaders; + configObject.xhr_headers = this.xhrHeaders; } - posthog.init(this.teamApiKey, configObject); - if(this.superProperties && Object.keys(this.superProperties).length > 0){ - posthog.register(this.superProperties); + } + + /** + * superproperties should be part of rudderelement.message.integrations.POSTHOG object. + * Once we call the posthog.register api, the corresponding property will be sent along with subsequent capture calls. + * To remove the superproperties, we call unregister api. + */ + processSuperProperties(rudderElement){ + const integrations = rudderElement.message.integrations; + if(integrations && integrations.POSTHOG){ + const {superProperties, setOnceProperties, unsetProperties} = integrations.POSTHOG; + if(superProperties && Object.keys(superProperties).length > 0){ + posthog.register(superProperties); + } + if(setOnceProperties && Object.keys(setOnceProperties).length > 0){ + posthog.register_once(setOnceProperties); + } + if(unsetProperties && unsetProperties.length > 0){ + unsetProperties.forEach(property => { + if(property && property.trim() != ""){ + posthog.unregister(property); + } + }); + } } + } identify(rudderElement) { @@ -83,12 +99,10 @@ class Posthog { const { userId } = rudderElement.message; if (userId) { - window.posthog.identify(userId); + posthog.identify(userId, traits); } - if (traits) { - window.posthog.people.set(traits); - } + this.processSuperProperties(rudderElement); } track(rudderElement) { @@ -96,7 +110,9 @@ class Posthog { const { event, properties } = rudderElement.message; - window.posthog.capture(event, properties); + this.processSuperProperties(rudderElement); + + posthog.capture(event, properties); } /** @@ -107,7 +123,9 @@ class Posthog { page(rudderElement) { logger.debug("in Posthog page"); - window.posthog.capture('$pageview'); + this.processSuperProperties(rudderElement); + + posthog.capture('$pageview'); } isLoaded() { From 3933de180e2e144fb6b96bb4f422b450b89ef78a Mon Sep 17 00:00:00 2001 From: prabrishac Date: Thu, 4 Feb 2021 14:58:37 +0530 Subject: [PATCH 4/4] updated test file --- integrations/Posthog/test/test_amplitude.html | 158 ------------------ integrations/Posthog/test/test_posthog.html | 54 ++++++ 2 files changed, 54 insertions(+), 158 deletions(-) delete mode 100644 integrations/Posthog/test/test_amplitude.html create mode 100644 integrations/Posthog/test/test_posthog.html diff --git a/integrations/Posthog/test/test_amplitude.html b/integrations/Posthog/test/test_amplitude.html deleted file mode 100644 index d9cf029c60..0000000000 --- a/integrations/Posthog/test/test_amplitude.html +++ /dev/null @@ -1,158 +0,0 @@ - - - - - - - -

Relax while I finish my testing process....

- - \ No newline at end of file diff --git a/integrations/Posthog/test/test_posthog.html b/integrations/Posthog/test/test_posthog.html new file mode 100644 index 0000000000..c7a35fa7cd --- /dev/null +++ b/integrations/Posthog/test/test_posthog.html @@ -0,0 +1,54 @@ + + + + + + + +

Relax while I finish my testing process....

+ + \ No newline at end of file