Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Push PHP variable to DataLayer

I am new to PHP and Pushing variables to the Datalayer and for marketing reasons I am trying to push this to the data layer, what I have done is below

I have a form where I grab the variables from the form and store them in PHP variables, I then JSON encoded them to appear in a JSON format and push them to the console to view them it:

$myObj = new stdClass();
$myObj -> name = $firstName;
$myObj -> surname = $lastName;
$myObj -> number = $Number;
$myObj -> LRP = $response;
                                    
$myJSON = json_encode($myObj);
                                    
echo '<script type="text/javascript">' .
     'console.log(' . $myJSON . ');</script>';
} 

I then echo the data in a JS script using quotations which output it to the console for me in this format

"name": "John",
"surname": "Doe",
"number": "1234567890",
"LRP": "Success ID = 1"

I have tried the same method as above to then push this to the DataLayer but it keeps giving me the unexpected token error because the format isn't correct but I am unsure of what is wrong, my code is below:

echo '<script type="text/javascript">' .
     'var datalayer = window.datalayer || [];
     dataLayer.push({
                                             
     (' . $myJSON . ')
                                         
     });
                                         
     </script>';

Is there any other form of documentation I can look at on how to push a PHP variable to the Datalayer or a fix inside my code?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda