Skip to content

Commit

Permalink
Segment Anonymous ID for Cloud UI Consumption (#1086)
Browse files Browse the repository at this point in the history
* custom generated ID => Segment Anonymous ID, tagging on analytics load, adding mobile nav click tracking

* qdrant-tech => ajs_anonymous_id

* remove unneeded exports
  • Loading branch information
jasonbryant84 authored Aug 14, 2024
1 parent 051f6e9 commit 66f58a1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
14 changes: 4 additions & 10 deletions qdrant-landing/themes/qdrant-2024/assets/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,11 @@ export function devLog(str) {
}
}

export const CROSS_SITE_URL_PARAM_KEY = 'qdrant-tech';
export function tagCloudUILinksWithHash() {
const CROSS_SITE_URL_PARAM_KEY = 'ajs_anonymous_id';
export function tagCloudUILinksWithAnonymousId() {
const targetUrl = 'https://cloud.qdrant.io/';

// Generate a random 32-character string
const generateRandomString = (length = 32) => {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
return Array.from({ length }, () => charset.charAt(Math.floor(Math.random() * charset.length))).join('');
};

const parameterValue = generateRandomString();
const anonymousId = analytics.user().anonymousId();

// Function to add or update query parameter in the URL
function addOrUpdateQueryParam(url, paramName, paramValue) {
Expand All @@ -101,6 +95,6 @@ export function tagCloudUILinksWithHash() {

// Loop through all selected <a> elements and update their href
links.forEach(link => {
link.href = addOrUpdateQueryParam(link.href, CROSS_SITE_URL_PARAM_KEY, parameterValue);
link.href = addOrUpdateQueryParam(link.href, CROSS_SITE_URL_PARAM_KEY, anonymousId);
});
}
7 changes: 2 additions & 5 deletions qdrant-landing/themes/qdrant-2024/assets/js/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import scrollHandler from './scroll-handler';
import { XXL_BREAKPOINT } from './constants';
import { initGoToTopButton, getCookie, tagCloudUILinksWithHash } from './helpers';
import { loadSegment, createSegmentStoredPage, tagAllAnchors } from './segment-helpers'
import { initGoToTopButton, getCookie } from './helpers';
import { loadSegment, createSegmentStoredPage } from './segment-helpers'

createSegmentStoredPage();

// on document ready
document.addEventListener('DOMContentLoaded', function () {
tagCloudUILinksWithHash();
tagAllAnchors();

if (!window.analytics && getCookie('cookie-consent')) {
loadSegment();
}
Expand Down
16 changes: 8 additions & 8 deletions qdrant-landing/themes/qdrant-2024/assets/js/segment-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCookie, devLog, CROSS_SITE_URL_PARAM_KEY } from './helpers';
import { getCookie, devLog, tagCloudUILinksWithAnonymousId } from './helpers';

const WRITE_KEY = 'segmentWriteKey';
const PAGES_SESSION_STORAGE_KEY = 'segmentPages';
Expand Down Expand Up @@ -33,16 +33,11 @@ const nameMapper = (url) => { // Mapping names based on pathname for Segment
/* DOM helpers */
/***************/
const handleClickInteraction = (event) => {
const url = new URL(event.target.href);
const searchParams = url.searchParams;
const qdrantTechHash = searchParams.get(CROSS_SITE_URL_PARAM_KEY);

const payload = {
...PAYLOAD_BOILERPLATE,
location: event.target.getAttribute('data-metric-loc') ?? '',
label: event.target.getAttribute('data-metric-label') ?? event.target.innerText,
action: 'clicked',
qdrant_tech_hash: qdrantTechHash ?? null
action: 'clicked'
};

// If consented to tracking the track
Expand All @@ -60,7 +55,7 @@ const handleClickInteraction = (event) => {

// Gather all <a> elements that have been tagged
// for tracking via 'data-metric-loc' attribute
export function tagAllAnchors() {
function tagAllAnchors() {
const allMetricsAnchors= document.querySelectorAll('a[data-metric-loc]');

if (allMetricsAnchors) {
Expand Down Expand Up @@ -250,6 +245,11 @@ export function loadSegment() {
analytics._writeKey = writeKey;
analytics.SNIPPET_VERSION = "5.2.0";
analytics.load(writeKey);

analytics.ready(function() {
tagCloudUILinksWithAnonymousId();
tagAllAnchors();
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
{{ end }}
</ul>
<div class="menu-mobile__controls">
<a href="{{ .Params.logIn.url }}" class="button button_outlined button_lg menu-mobile__login"
<a data-metric-loc="mobile_nav" href="{{ .Params.logIn.url }}" class="button button_outlined button_lg menu-mobile__login"
>{{ .Params.logIn.text }}</a
>
<a href="{{ .Params.startFree.url }}" class="button button_contained button_lg">{{ .Params.startFree.text }}</a>
<a data-metric-loc="mobile_nav" href="{{ .Params.startFree.url }}" class="button button_contained button_lg">{{ .Params.startFree.text }}</a>
</div>
</div>
{{ end }}

0 comments on commit 66f58a1

Please sign in to comment.