How do you track Caps Lock users with Google Analytics? - javascript

I enjoy working with Google Analytics and the ways that I am able to slice information about our visitors. We use Customer Variables to track information about who and how are users interact with our site. Staying true to the goal of Analytics, we are always looking for ways to improve and optimize our website.
Currently we are in a phase of development where we can make choices about how we want to store and present product information. One of the questions that came up was whether or not to show product information in all caps or not. Working with our users for the last few years, it seems that much of our traffic comes from visitors who do have caps lock on. So it got us thinking, could we track our caps lock users with a customer variable to that we can make a more informed determination about how to present the information?
Check out this sample I slapped together: http://jsfiddle.net/shanabus/Za4kL/
Our site basically represents a standard e-commerce site. There are a few different text boxes and that allow you to search for part numbers and throughout the order process there are a few places where users can type text. Would you bind the caps lock test to all text boxes or just the common ones? Is there a performance hit if I bind the keypress listener to all text boxes on the site or is it negligible? Is there a better way to implement this?
I imagine instead of showing/hiding a div I would instead set the custom var:
_gaq.push('_setCustomVar', 5, 'capslock', 'true', 3);
Thanks for any thoughts and consideration on this seemingly trivial topic.

I'd bind the event globally, and use the following code:
var CAPS_ON = null;
$(window).keypress(function(ev) {
var charCode = ev.which; //jQuery normalizes ev.charCode to ev.which
// Lowercase chars
if (charCode >= 97 && charCode <= 122) {
CAPS_ON = ev.shiftKey; // Caps are off if SHIFT is not pressed
} else if (charCode >= 65 && charCode <= 90) {
CAPS_ON = !ev.shiftKey;
}
});
This creates a variable CAPS_ON, which can be used throughout the page.
Further explanation on the code:
The event has to be bound to the keypress event, because it's the only key event which discerns lowercase/uppercase characters.
The shiftKey property has to be checked, because it inverts the CAPS LOCK feature.

Related

Possible to have two conditions for an IF statement in javascript?

Alright so I am currently making a PBBG based game. However, I have been having trouble with my function for a button click and two conditions in my if statement. I am actually not positive if this is at all possible, I searched everywhere I could think of but nothing gave me a definitive answer for getting this to work.
Basically, what I am trying to achieve, is that when a player presses the 'Attack' button, the player then receives the amount of experience points and gold they get from defeating that monster. Then, after that function runs, I am setting a delay of 6 seconds to where they can't press the button to attack again until the 6 seconds have passed.
I did get the function and onClick to work where when they win the fight, the game awards them the experience and gold from the kill. That all worked great and I made sure that was all working BEFORE I started adding in the time delay function and all.
Here is my code for the function with the time delay I am trying to add: Code
(Won't allow me to embed pictures yet so a link will have to do for now) and I am using just an HTML button with the onClick value set to SingleAttack(). The code with the problem appears to be in this part...
if (attackReady) || (currentExp >= NeededExp) {...}
What I have done here is I am holding a boolean, named attackReady and setting it to 'true'. When the player presses the Attack button, it then changes the 'true' value to 'false' and then adds a setTimeout() and period in miliseconds for delay. It then sets attackReady back to true and puts a 6 second time delay before the function can be called again.
You'll notice there is an if and an else if in my function. The if code runs only when a players current experience points are greater than or equal to the needed experience points. I think my problem is coming from having two conditions in the if statement. I am not entirely sure if that is possible, I have looked everywhere to find an answer and nothing for javascript specifically. I know C# allows it, does javascript allow it and if so, did I do it right or have I done it wrong?
When the Attack button is clicked and the function is called, it does nothing at all. Any insights?
As #Thomas noted, the entire test needs to also be enclosed in braces. You currently have:
if (attackReady) || (currentExp >= NeededExp) {...}
and what you want is either:
if ( (attackReady) || (currentExp >= NeededExp) ) {...}
or more simply:
if (attackReady || currentExp >= NeededExp) {...}
One more thing:
From your description, it might be the case that you want both tests to be true to execute that block of code. If that is the case you want to use an AND with && rather than the || OR test. OR is true if either the left or the right hand side is true.

Will this work against cheat engine?

I started programming a few months ago. I'm making a complete client side game in Animate CC, so I'm trying a simple measure against memory scan software.
I'm trying to avoid people to change my money variable.
var canMoneyChange = false;
var money = 0;
var previousMoney = 0;
function everyFrame() { //Let's admit that this function is called every frame
if (moneyChange == true) {
lastMoney = money;
canMoneyChange = false;
} else {
if (lastMoney != money) { //If money is "magically" changed it should drop here
resetGame();
}
}
Now evertime I update the money visual display I also have to include the boolean variable:
//...
canMoneyChange = true;
money += 100; //For example
updateMoney(); //This is only for visual effects
//...
Wondering if this works at all, thanks.
EDIT: Oh damn, I was not realising that CE would find both lastMoney and money at the same time. I could do something like multiplying by a number to hide lastMoney:
function everyFrame() { //Let's admit that this function is called every frame
if (moneyChange == true) {
lastMoney = money * 8;
canMoneyChange = false;
} else {
if (lastMoney != money * 8) {
resetGame();
}
}
This will stop 50% of Cheat Engine users because most users are inexperienced and are only capable of doing simple scans and memory modifications. They will just give up because you've raised the adversarial costs above their threshold.
As others have commented, it's a cat and mouse game.
Users can still scan for "unknown initial value" and scan for decreased and increased values. This will yield the obfuscated money value and the regular value, doesn't take too much to figure it out from there.
Also users can do "Find what Writes to this address" that will put a write breakpoint on the money address, it will then give them the instruction that changes the money back to the original value. At this point they will see the:
lastMoney = money * 8;
in assembly and be able to figure it out from there.
In all anti-cheat situations, each deterrent you put in place will raise adversarial costs and filter out another tier of cheaters. Your goal should never be to stop all cheaters 'cuz that's never happening. But in a few hours you can roll up a bit of obfuscation and a couple anti-debug measures to deter 75% of the cheaters. Problem is when the other 25% representing the experienced cheaters release the cheats. At that point the 75% inexperienced group's adversarial costs represent a search on a search engine.
I would say add some IsDebuggerPresent() type checks but I imagine on your platform that's not possible.
I'm not familiar with Animate CC or Flash, but combining 1 custom obfuscation technique like you're working on right now, with a public free obfuscator will annoy a substantial number of people enough to give up.

Certain key combinations prevent key events in javascript

Working on an app that requires the ability to hold down multiple keys at once to trigger unique functions.
I've run into a situation where certain combinations of keys will prevent 'keydown' from being triggered.
Holding a horizontal row 1 > 2 > 3 > 4 > 5 > 6 or a vertical column 1 > Q > A > Z
(6 keys appears to be the max the browser will recognise at once) will work however - if a user holds corner shape for example 1 > 2 > w the events are prevented.
Can be demonstrated using this fiddle:
http://jsfiddle.net/B1KMusic/U5L2X/light/
Interestingly though this 'shape' of keys appears significant - 2 > 3 > Wand 3 > 4 > R etc. will also be prevented.
Even if this shape is rotated across the keyboard the same shapes like: C -> X -> S and N -> H -> J will do the same.
If a gap is left and the corner is not 'connected' then the events will work properly 3 > E > D > V - but if C is pressed this will not work.
What is going on here? Is this some intentional browser default to prevent key mashing?
Edit: As the answer provided points out, this is a hardware issue so it makes sense to include hardware info: tested on a late 2013 Macbook Pro, apple stackexchange question confirms the answer here: https://apple.stackexchange.com/questions/47699/are-apple-keyboards-multi-key-rollover
This is a hardware limitation of your keyboard, not an issue with the browser. Most keyboards use a matrix arrangement for key switches, and hence cannot detect certain combinations of keys being pressed. The specific combinations which will and will not work are hardware-dependent, but in general, it is not safe to assume that more than two non-modifier keys can be pressed simultaneously.
Some gaming keyboards support n-key rollover (that is, any number of keys being pressed at once), but these are rare.

Adobe Livecycle cacluations and if statements

my first time posting, and tbh I have very little Xp. I'm using livecycle for Adobe 9 pro, and trying to make a calculation work, and keep getting error messages.
My basic premise I need to do a calculation: enter info in cella, and have result of (cella/2)-5 rounded down, keeping the negative integer come out in another cell. (yes, trying to do my own 3.5 d20 character sheet for ability scores).
In excel I was able to string a slightly more complicated trunc formula of =IF((Cella-10)/2<0,TRUNC((Cella)/2-0.5),TRUNC((Cella-10)/2)), but have no idea what to do in livecycle.
I tried something like this following a tutorial for livecycle, with no avail. honestly noobing it here on all accounts including where notations and variables should be, thanks for the help.
var x = cell1/2-5; if (x <0) {return Math.ceil(x)}; else {return Math.floor(x)}
//cell1 pick from sheet in livecycle using control+click, tried calculation and enter formats on script line. //Do I need a var x for my formula, not sure, some tutorials said yes, others no for live cycle. //not sure where to put { } if at all. // Math.floor and Math.ceil do not show up highlighted blue like other functions do in the livecycle script bar, which I leave in javamode for all cells.
Previous answer has got the math right but it won't work in a LiveCycle event (return statement is invalid in an event.)
Let's assume you have two fields: cellA (where you input numbers), cellB (where the rounded result should display). Replace with the actual names in your sheet as necessary.
In the calculate event for cellB (the field that holds the RESULT) put in this code:
var v = null;
if (cellA.rawValue != null && cellA.rawValue != "")
{
v = cellA.rawValue / 2 - 5;
v = v < 0 ? Math.ceil(v) : Math.floor(v);
}
this.rawValue = v;
The if statement is there so that it won't calculate when you first open the form.
Side note, the javascript editor in livecycle pretty much sucks so don't expect a lot of help with syntax/formatting. I would recommend you download Notepad++ and paste the java code in there, it is more helpful for highlighting some syntax and for checking open/closed parentheses.

Can I query/detect the double click speed for a webpage user?

It's OS/user dependant. Not the browser, not the website, but the OS decides how fast and slow a double click must be.
I'd like to use that number in my app. Is there a way to get that number with JS?
Simple question. Might not be possible.
Thanks
Simple answer: no, sorry.
The best you could do would be something like this (example uses jQuery simply because it was quicker to write, the principle holds if jQuery is unavailable. Also note that this could well be simplified, this is just what came to mind first):
var timer,
num = 0;
$("#example").click(function() {
/*This condition is required because 2 click events are fired for each
dblclick but we only want to record the time of the first click*/
if(num % 2 === 0) {
timer = (new Date()).getTime();
}
num++;
}).dblclick(function() {
var time2 = (new Date()).getTime(),
dblClickTime = time2 - timer;
});
Unfortunately, that's probably not very helpful. You may be able to record the dblClickTime values and check for the longest, but that still is very unlikely to be the actual value you're after. That sort of thing is just not available through JavaScript.
Answer 2021 - as far as I know - still not. There is a reason: we should not care.
In principle dblclick is somehow obsolete …
We have the not well known detail property. Maybe because of the name.
From MDN:
The MouseEvent object passed into the event handler for click has its detail property set to the number of times the target was clicked. In other words, detail will be 2 for a double-click, 3 for triple-click, and so forth. This counter resets after a short interval without any clicks occurring; the specifics of how long that interval is may vary from browser to browser and across platforms. The interval is also likely to be affected by user preferences; for example, accessibility options may extend this interval to make it easier to perform multiple clicks with adaptive interfaces.
With detail ie. click_count it is possible to stop propagation of CLICK when detail != 1
So pseudcode:
if evt.detail==1
do_click()
if evt.detail==2
do_dblclick()
...
if evt.detail!=1
evt.stopPropagation()
If someone really needs to distinguish between click, double-click, triple-click, … like an 'XOR', they should really rethink the design.
The DblClickTime can be very long, that means the app feels like not responding, if the user just wants the click-action.
The other problem is, that it is possible, that users intention is a double-click, but is to slow - then there are two click-actions, they should not be to different to dblclick.
I'd like to use that number in my app. Is there a way to get that number with JS?
Definitely not - stuff like this is outside JavaScript's scope.
You may be able to find out values that work for a double click by asking the user to double-click, listen to the click events and see whether the dblclick event is fired - I'm nnot sure whether event handling works that way, though. But even if that works, it is still a long way from actually finding out the actual value.
This is my 2015 solution, would like to see a pure js version tho.
var start;
var click = null;
$(document).click(function() {
var now = performance.now();
start = click ? click : now;
click = now;
}).dblclick(function() {
alert(performance.now()-start)
});
EDIT
Pure JS
var start;
var click = null;
var getStart = function() {
var now = performance.now();
start = click ? click : now;
click = now;
}
var getStop = function() {
alert(performance.now()-start)
}
if (window.addEventListener) {
window.addEventListener('click', getStart , false);
} else {
window.attachEvent('onclick', function() {
return(getStart.call(window, window.event));
});
}
if (window.addEventListener) {
window.addEventListener('dblclick', getStop , false);
} else {
window.attachEvent('ondblclick', function() {
return(getStop.call(window, window.event));
});
}
Adding on to James Allardice's answer:
Depending on your implementation and where you are looking for double clicks you may want to also check the users mouse location (or I guess tap location). This is to avoid a double click firing when the user is clicking things on different parts of your page (again depends on your event listener implementation -- if it is just on one button for example this probably isn't an issue).
When a click event fires the event listener in my example below has two variables e.clientX and e.clientY. This will give you the location of the mouse. You might want to check to see if the user has moved their mouse significantly since the first click (adapt accordingly to your code).
document.addEventListener("click", function(e){ console.log("Mouse X: " + e.clientX + ": Mouse Y: " + e.clientY); });
You don't want to have it be too tight or else a user may never be able to fire a double click, and you don't want it to be too loose so that double clicks fire seemingly randomly for the user. Maybe start with a 25px or so box around the first click (again this depends on your application). This is something you can test and adjust based on your user interface.
I am assuming you don't have jQuery or aren't using it, because I believe jQuery might already do this calculation to fire dblclick

Categories