Prevent sending of Google event when pressed F5 and access via GET

0

Speak up!

I need to do an event upload prevention of analitycs in the following scenario:

I have a form that when the submit is given, the post is rendered in ajax and this ajax returns a URL in which I do the redirect via location.href . This other page where you receive the targeting has the code snippet where you send the event to Google. What happens is that every time the F5 user or directly enters the link, the event is being counted, giving wrong results in Analitycs reports.

Here's an example:

Source Page:

<form id="form">
  <input type="text" name="teste" />
  <button>Processar</button>
</form>

Landing page:

<script>
 // preciso prevenir esse envio quando usuário der F5 ou entrar na página sem passar
 // pelo form
 ga('send', 'event', 'Teste', 'view', '<?php echo $_GET['varTeste'] ?>');
</script>

Script that does the redirect:

$( "#form" ).submit(function() {
  // ajax na qual retorna o link
  url = 'teste.php?varTeste=123';
  location.href = url;
});
    
asked by anonymous 18.05.2017 / 16:01

1 answer

1

You could add the script to the DOM only if a specific parameter is present in $_GET .

<?php if (isset($_GET['varTeste'])): ?>
    <script>...</script>
<?php endif ?>

And then you can remove this parameter via JavaScript, so when the page was reloaded, or accessed without the parameter the script would not be added to the DOM.

$(document).ready(function () {
    // remove os parametros GET
    window.history.replaceState(null, null, window.location.pathname);
});

See replaceState () .

  

If necessary, add setTimeout() to delay removal of the url parameter.

    
18.05.2017 / 17:27