Skip to content

Commit

Permalink
[SAT-433] Measure Field Data in Google Analytics (#551)
Browse files Browse the repository at this point in the history
Measure Field Data in Google Analytics
  • Loading branch information
filipechagas authored Sep 14, 2021
1 parent 7238de7 commit 8443122
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Public/assets/main.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"mini-css-extract-plugin": "^1.6.0",
"style-loader": "^2.0.0",
"superagent": "^5.2.2",
"web-vitals": "^2.1.0",
"webpack": "^5.40.0",
"webpack-cli": "^4.7.2"
}
Expand Down
238 changes: 135 additions & 103 deletions src/js/app.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,147 @@
import $ from 'jquery';

$(function () {

$.ajax(window.BASE_PREFIX + '/dropdown/templates.json').done(function(data) {
// bootstrap select2
$("#input-gitignore").select2({
data: data,
theme: "toptal", // customized theme
multiple: true,
allowClear: false,
minimumInputLength: 1,
selectOnClose: true,
placeholder: $("#input-gitignore-placeholder").text(),
sorter: function(results) {
const query = $('.select2-search__field').val().toLowerCase();
return results.sort(function(a, b) {
return a.text.toLowerCase().indexOf(query) - b.text.toLowerCase().indexOf(query);
});
},
});

// load pre-selected tags from URL search params
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("templates") != null) {
const preFilledSearchTerms = urlParams.get("templates").replace(/\s/g, "+").toLowerCase().split(",");
const validIDs = data.map(function(datum) {
return datum.id
});
const validPreFilledSearchTerms = preFilledSearchTerms.filter(function(term) {
return validIDs.indexOf(term) >= 0;
});
$("#input-gitignore").val(validPreFilledSearchTerms).trigger('change.select2');
} else {
// in order to fix the problem where placeholder gets cut off
$("#input-gitignore").val('').trigger('change.select2');
}

// Highlight input on site load
setTimeout(function() {
$(".select2-search__field").focus();
// prevent dropdown opening on page load focus
$("#input-gitignore").on("select2:opening", function (e) {
e.preventDefault();
$("#input-gitignore").off("select2:opening");
});
});

import $ from "jquery";
import { getCLS, getFID, getLCP } from "web-vitals";
export class GoogleAnalytics {
sendToGoogleAnalytics({ name, delta, id }) {
window.gtag("event", name, {
event_category: "web_vitals",
event_label: id,
value: Math.round(name === "CLS" ? delta * 1000 : delta),
non_interaction: true,
});
}

trackCoreWebVitals() {
getCLS(this.sendToGoogleAnalytics);
getFID(this.sendToGoogleAnalytics);
getLCP(this.sendToGoogleAnalytics);
}
}

// All users to press ctrl+enter to create template
$("#input-gitignore").on("select2:selecting", function(e) {
setTimeout(function() {
$(".select2-search__field").keydown(function(e) {
if (e.keyCode == 13 && (e.metaKey || e.ctrlKey)) {
generateGitIgnore();
}
});
$(function () {
$.ajax(window.BASE_PREFIX + "/dropdown/templates.json").done(function (data) {
const GAInstance = new GoogleAnalytics();
GAInstance.trackCoreWebVitals();

// bootstrap select2
$("#input-gitignore").select2({
data: data,
theme: "toptal", // customized theme
multiple: true,
allowClear: false,
minimumInputLength: 1,
selectOnClose: true,
placeholder: $("#input-gitignore-placeholder").text(),
sorter: function (results) {
const query = $(".select2-search__field").val().toLowerCase();
return results.sort(function (a, b) {
return (
a.text.toLowerCase().indexOf(query) -
b.text.toLowerCase().indexOf(query)
);
});
},
});

// load pre-selected tags from URL search params
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("templates") != null) {
const preFilledSearchTerms = urlParams
.get("templates")
.replace(/\s/g, "+")
.toLowerCase()
.split(",");
const validIDs = data.map(function (datum) {
return datum.id;
});
const validPreFilledSearchTerms = preFilledSearchTerms.filter(function (
term
) {
return validIDs.indexOf(term) >= 0;
});
$("#input-gitignore")
.val(validPreFilledSearchTerms)
.trigger("change.select2");
} else {
// in order to fix the problem where placeholder gets cut off
$("#input-gitignore").val("").trigger("change.select2");
}

// prevent auto sorting of tags selection, keep the order in which they are added
// @ref https://github.com/select2/select2/issues/3106
$("#input-gitignore").on('select2:select', function(e) {
var id = e.params.data.id;
var option = $(e.target).children('[value="'+id+'"]');
option.detach();
$(e.target).append(option).change();
});


// bind click handler to "Create" button
$("#btn-gitignore").click(function() {
generateGitIgnore()
});


// Delete selections by tag instead of individual letter
$.fn.select2.amd.require(['select2/selection/search'], function (Search) {
Search.prototype.searchRemoveChoice = function (decorated, item) {
this.trigger('unselect', {
data: item
});

this.$search.val('');
this.handleSearch();
};
// Highlight input on site load
setTimeout(function () {
$(".select2-search__field").focus();
// prevent dropdown opening on page load focus
$("#input-gitignore").on("select2:opening", function (e) {
e.preventDefault();
$("#input-gitignore").off("select2:opening");
});
});


// Generate gitignore template
function generateGitIgnore() {
const searchString = $("#input-gitignore").map(function() {return $(this).val();}).get().join(',');
const searchLength = searchString.length;
if (searchLength > 0) {
const files = searchString.replace(/^,/, '');
window.location = window.BASE_PREFIX + '/api/' + files;
$("#input-gitignore").val("");
});

// All users to press ctrl+enter to create template
$("#input-gitignore").on("select2:selecting", function (e) {
setTimeout(function () {
$(".select2-search__field").keydown(function (e) {
if (e.keyCode == 13 && (e.metaKey || e.ctrlKey)) {
generateGitIgnore();
}
});
});
});

// prevent auto sorting of tags selection, keep the order in which they are added
// @ref https://github.com/select2/select2/issues/3106
$("#input-gitignore").on("select2:select", function (e) {
var id = e.params.data.id;
var option = $(e.target).children('[value="' + id + '"]');
option.detach();
$(e.target).append(option).change();
});

// bind click handler to "Create" button
$("#btn-gitignore").click(function () {
generateGitIgnore();
});

// Delete selections by tag instead of individual letter
$.fn.select2.amd.require(["select2/selection/search"], function (Search) {
Search.prototype.searchRemoveChoice = function (decorated, item) {
this.trigger("unselect", {
data: item,
});

this.$search.val("");
this.handleSearch();
};
});

// Generate gitignore template
function generateGitIgnore() {
const searchString = $("#input-gitignore")
.map(function () {
return $(this).val();
})
.get()
.join(",");
const searchLength = searchString.length;
if (searchLength > 0) {
const files = searchString.replace(/^,/, "");
window.location = window.BASE_PREFIX + "/api/" + files;
$("#input-gitignore").val("");
}


// Generate gitignore file template
function generateGitIgnoreFile() {
const searchString = $("#input-gitignore").map(function() {return $(this).val();}).get().join(',');
const searchLength = searchString.length;
if (searchLength > 0) {
const files = searchString.replace(/^,/, '');
window.location = window.BASE_PREFIX + '/api/f/' + files;
}
}

// Generate gitignore file template
function generateGitIgnoreFile() {
const searchString = $("#input-gitignore")
.map(function () {
return $(this).val();
})
.get()
.join(",");
const searchLength = searchString.length;
if (searchLength > 0) {
const files = searchString.replace(/^,/, "");
window.location = window.BASE_PREFIX + "/api/f/" + files;
}
}
});

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5293,6 +5293,11 @@ watchpack@^2.2.0:
glob-to-regexp "^0.4.1"
graceful-fs "^4.1.2"

web-vitals@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/web-vitals/-/web-vitals-2.1.0.tgz#ebf5428875ab5bfc1056c2e80cd177001287de7b"
integrity sha512-npEyJP8jHf3J71t1tRTEtz9FeKp8H2udWJUUq5ykfPhhstr//TUxiYhIEzLNwk4zv2ybAilMn7v7N6Mxmuitmg==

webidl-conversions@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
Expand Down

0 comments on commit 8443122

Please sign in to comment.