-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SAT-433] Measure Field Data in Google Analytics (#551)
Measure Field Data in Google Analytics
- Loading branch information
1 parent
7238de7
commit 8443122
Showing
4 changed files
with
142 additions
and
104 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
}); | ||
|
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