-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Rudderstack tracking + fix classNames in js files (#1686)
* added Rudderstack tracking + fix classNames in js files * improved tracking of pages and make it silent
- Loading branch information
Showing
9 changed files
with
205 additions
and
45 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
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
File renamed without changes.
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,153 @@ | ||
import { nanoid } from 'nanoid'; | ||
import { createHash } from 'crypto'; | ||
import { get, split, truncate } from 'lodash'; | ||
import { RudderAnalytics } from '@rudderstack/analytics-js'; | ||
|
||
export function initRudderAnalytics() { | ||
const rudderAnalytics = new RudderAnalytics(); | ||
window.rudderanalytics = rudderAnalytics; | ||
rudderAnalytics.load("2bP8Ev07SiL1ABLXhcru4JZQ5Uh", "https://near.dataplane.rudderstack.com", {}); | ||
rudderAnalytics.setAnonymousId(getAnonymousId()); | ||
return rudderAnalytics; | ||
} | ||
|
||
export function recordPageView(rudderAnalytics, pageName) { | ||
if(pageName === '/') return; | ||
if(!rudderAnalytics) return; | ||
|
||
// pageName should be /category/page and never contain ? or # | ||
// but just in case we split on ? and # | ||
const split = pageName.split('?')[0].split('#')[0].split('/'); | ||
const category = split[1]; | ||
const page = split.slice(2).join('/'); | ||
try { | ||
rudderAnalytics.page(category, page, { | ||
hashId: localStorage.getItem('hashId') || '', | ||
url: filterURL(window.location.href), | ||
ref: filterURL(document.referrer), | ||
}); | ||
} catch (e) { } // Silent error | ||
} | ||
|
||
// let rudderAnalytics = null; | ||
// let hashId = ''; | ||
// let pendingEvents = []; | ||
|
||
// export function setAccountIdHash(accountId) { | ||
// const hash = createHash('sha512'); | ||
// hash.update(accountId); | ||
// hashId = hash.digest('hex'); | ||
// localStorage.setItem('hashId', hashId); | ||
// } | ||
|
||
export function getAnonymousId() { | ||
const storageId = localStorage.getItem('anonymousUserId'); | ||
|
||
if (storageId) { | ||
console.log("Existing storageId", storageId); | ||
return storageId; | ||
} | ||
|
||
console.log("Creating new storageId"); | ||
const anonymousUserId = nanoid(); | ||
const anonymousUserIdCreatedAt = new Date().toUTCString(); | ||
localStorage.setItem('anonymousUserId', anonymousUserId); | ||
localStorage.setItem('anonymousUserIdCreatedAt', anonymousUserIdCreatedAt); | ||
|
||
return anonymousUserId; | ||
} | ||
|
||
function isStringAllowed(str) { | ||
const denyList = ['account_id', 'public_key', 'all_keys', 'publicKey', 'apiKey', 'accountId', 'email']; | ||
return !str || !denyList.some((param) => str.indexOf(param) !== -1); | ||
} | ||
|
||
function filterURL(url) { | ||
const [urlTrim, params] = split(url, '?'); | ||
return isStringAllowed(params) ? url : urlTrim; | ||
} | ||
|
||
|
||
// const record = (eventType, e) => { | ||
// const key = get(e.target, 'placeholder', get(e.target, 'innerText', get(e.target, 'href'))); | ||
// recordEventWithProps(eventType, { | ||
// element: truncate(key, { length: 255 }), | ||
// url: e.target ? filterURL(e.target.baseURI) : '', | ||
// xPath: getXPath(e.target), | ||
// componentSrc: getComponentName(e.target), | ||
// }); | ||
// }; | ||
|
||
// export const recordClick = (e) => record('click', e); | ||
// export const recordMouseEnter = (e) => record('mouseover', e); | ||
// export const recordTouchStart = (e) => record('touchstart', e); | ||
|
||
// export function recordWalletConnect(accountId) { | ||
// if (!localStorage.getItem('hashId')) { | ||
// setAccountIdHash(accountId); | ||
// recordEvent('wallet-connected'); | ||
// } | ||
// } | ||
|
||
// export function reset() { | ||
// if (!rudderAnalytics) return; | ||
// try { | ||
// recordEvent('wallet-logout'); | ||
// localStorage.removeItem('hashId'); | ||
// localStorage.removeItem('anonymousUserId'); | ||
// localStorage.removeItem('anonymousUserIdCreatedAt'); | ||
// rudderAnalytics.reset(); | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
// } | ||
|
||
// export function recordEventWithProps(eventLabel, properties) { | ||
// if (!rudderAnalytics) return; | ||
// try { | ||
// rudderAnalytics.track(eventLabel, { | ||
// ...properties, | ||
// hashId: localStorage.getItem('hashId'), | ||
// anonymousUserIdCreatedAt, | ||
// }); | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
// } | ||
|
||
// export function recordEvent(eventLabel) { | ||
// if (!rudderAnalytics) return; | ||
// try { | ||
// rudderAnalytics.track(eventLabel, { | ||
// hashId: localStorage.getItem('hashId'), | ||
// url: window.location.href, | ||
// anonymousUserIdCreatedAt, | ||
// }); | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
// } | ||
|
||
// function getComponentName(element) { | ||
// if (!element) return ''; | ||
// if (element.hasAttribute('data-component')) return element.getAttribute('data-component') || ''; | ||
// return ''; | ||
// } | ||
|
||
// function getXPath(element) { | ||
// if (!element) return ''; | ||
// if (element.id !== '') return 'id("' + element.id + '")'; | ||
// if (element === document.body) return element.tagName; | ||
|
||
// let ix = 0; | ||
// const siblings = element.parentNode?.children; | ||
// if (!siblings) return ''; | ||
|
||
// for (let i = 0; i < siblings.length; i++) { | ||
// const sibling = siblings[i]; | ||
// if (sibling === element) return getXPath(element.parentElement) + '/' + element.tagName + '[' + (ix + 1) + ']'; | ||
// if (sibling.nodeType === 1 && sibling.tagName === element.tagName) ix++; | ||
// } | ||
|
||
// return ''; | ||
// } |
File renamed without changes.