Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!