1

I have this code inside a file located at http://example.com/analytics.js and referenced in the HMTL head. The below code is kind of Javascript pseudo code to what I want to do

function collectStatistics(){ // this function will send the page view to my server recordPageView(window.location.href); } 

How can I prevent malicious code and spammers from abusing my recordPageView function? Simplest thing they can do is to put it inside a loop in the browser console and if I throttle it, they can put a timer inside the loop ... etc

Thanks

1 Answer 1

5

You can't. Client-side code is entirely hackable, full stop. Anyone viewing the page can readily trigger calls to collectStatistics, in a loop or otherwise. Or they can take it a step further, see what you're sending in recordPageView, and do it directly.

All you can do is track this stuff server-side and look for patterns of abuse, which is exactly what Google and others do with their ad networks.

Sign up to request clarification or add additional context in comments.

5 Comments

So how google analytics and facebook and others are doing it and collecting their statistics and avoiding spammers? There should be something to avoid the noise can be caused by spammers!
Sorry I commented on your incomplete answer... Yes, I see
@Mutaz: LOL Sorry, I don't remember editing it (but I totally believe that I did).
@t-j-crowder do you have any idea about the patterns could be successfully applicable to this case?
@Mutaz: I'm afraid not, it's not one of my areas of expertise. It's a complex and endlessly changing subject, like spam.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.