简体   繁体   中英

How to track a form on submit with Google Analytics and PHP

I want to track a form with GA and setup GOAL's i need a proper code that work when the form is submited.

my code is:

<?php
if(isset($_GET['button'])){
  GA code here..
}

?>

there is difrent examples out there but, i need one thats actual work, so if anyone have example that work, im intressed in such.

Save the google analytics script in a file say analyticstracking.php, then include the file as follows:

<?php   include_once("analyticstracking.php"); ?>

The code to be saved in analyticstracking.php can be found from google analytics site.

Trivial answer, expanding on your own code example:

<?php
if(isset($_GET['button'])){
?>
<script>
ga('send', 'event', 'Fom', 'submit', 'some label');
</script>
<?php
}
?>

Closing and opening the php tags means the code in the if condition will only be executed when the condition is met. This is a less than elegant solution, but workable (as long as the tracker is created somewhere before in the page). It would be better to create a dedicate url for a thankyou-page and track that.

You need to call the track event api for ga 2 https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide for universal analytics https://developers.google.com/analytics/devguides/collection/analyticsjs/events

If you want to do it PHP side see this https://github.com/thomasbachem/php-ga

if jquery/ js then

for ga2

$( "form" ).submit(function() {

_gaq.push(['_trackEvent', 'formsubmit', 'form', 'form was submitted']);

});

or for universal analytics

$('form').submit(function() {
  ga('send', 'event', 'form', 'submit', 'form submitted');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM