function wpan_send_form_tracking_event($entry, $form) { global $post; $msg = "About to send form tracking event to Google Analytics..."; wpan_log_debug($msg); GFCommon::log_debug($msg); /* Extract the plugin options from the database */ $options = wpan_get_options(); $tracking_uid = isset($options['tracking_uid']) ? $options['tracking_uid'] : ''; $debug = isset($options['debug']) ? $options['debug'] : ''; $form_title = $form['title']; /* I have taken the following four lines of code from https://github.com/theiconic/php-ga-measurement-protocol; thank you! */ $document_path = str_replace(home_url(), '', $entry['source_url']); $document_location = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/' . $_SERVER['REQUEST_URI']; $document_title = isset($post) && get_the_title($post) ? get_the_title($post) : 'no title'; /* Setup the class */ $ga_options = ['client_create_random_id' => true, 'client_fallback_id' => 555, 'client_id' => null, 'user_id' => null, 'adapter' => ['async' => true, 'ssl' => false]]; /* Connect to tracker */ $gatracking = new \Racecore\GATracking\GATracking($tracking_uid, $ga_options); /* Build GA event */ $event = $gatracking->createTracking('Event'); $event->setAsNonInteractionHit(false); $event->setEventCategory('Contact'); $event->setEventAction($form_title); $event->setEventLabel($document_path); $event->setDocumentPath($document_path); $event->setDocumentLocation($document_location); $event->setDocumentTitle($document_title); /* Send event to GA severs */ $response = $gatracking->sendTracking($event); /* Debug */ if ($debug) { wpan_log_debug("Sent the following event to Google Analytics:"); wpan_log_debug($event); wpan_log_debug("Received the following respons from Google Analytics (ASYNC, so it might be empty): "); wpan_log_debug($response); // wpan_log_debug( "This is the form that triggered the event:" ); // wpan_log_debug( $form ); // wpan_log_debug( "This is the entry of the form in Gravity Forms:" ); // wpan_log_debug( $entry ); } }
<?php require_once dirname(__FILE__) . '/../src/Racecore/GATracking/GATracking.php'; /** * Setup the class */ $options = array('client_create_random_id' => true, 'client_fallback_id' => 555, 'client_id' => null, 'user_id' => null, 'adapter' => array('async' => true, 'ssl' => false)); $gatracking = new \Racecore\GATracking\GATracking('UA-XXXXXX-X', $options); /** @var Tracking/Factory $event */ $event = $gatracking->createTracking('Factory', array('dh' => 'example.com', 'dp' => '/path/foo', 'dt' => 'Example Title')); $response = $gatracking->sendTracking($event);
<?php require_once dirname(__FILE__) . '/../src/Racecore/GATracking/GATracking.php'; /** * Setup the class * optional */ $options = array('client_create_random_id' => true, 'client_fallback_id' => 555, 'client_id' => null, 'user_id' => null, 'adapter' => array('async' => true, 'ssl' => false)); $gatracking = new \Racecore\GATracking\GATracking('UA-XXXXXX-X', $options); /** @var Tracking/Event $event */ $event = $gatracking->createTracking('Event'); $event->setAsNonInteractionHit(true); $event->setEventCategory('ImageGallery'); $event->setEventAction('displayImage1'); /** @var Tracking/Event $event */ $event2 = $gatracking->createTracking('Event'); $event2->setEventCategory('Categorie'); $event2->setEventAction('clickCat5'); $response = $gatracking->sendMultipleTracking(array($event, $event2));
/** * Push the Google Analytics Event! * * @since 1.4.0 * @param array $event Gravity Forms event object * @param array $form Gravity Forms form object */ private function push_event($entry, $form, $ga_event_data) { //Get all analytics codes to send $google_analytics_codes = array(); if (!empty($ga_event_data['gaEventUA'])) { $ga_ua = explode(',', $ga_event_data['gaEventUA']); if (is_array($ga_ua)) { foreach ($ga_ua as &$value) { $value = trim($value); } } $google_analytics_codes = $ga_ua; } if ($this->ua_id) { $google_analytics_codes[] = $this->ua_id; } $google_analytics_codes = array_unique($google_analytics_codes); /** * Filter: gform_ua_ids * * Filter all outgoing UA IDs to send events to * * @since 1.6.5 * * @param array $google_analytics_codes UA codes * @param object $form Gravity Form form object * @param object $entry Gravity Form Entry Object */ $google_analytics_codes = apply_filters('gform_ua_ids', $google_analytics_codes, $form, $entry); $event = new \Racecore\GATracking\Tracking\Event(); // Set some defaults $event->setDocumentLocation($ga_event_data['document_location']); $event->setDocumentTitle($ga_event_data['document_title']); // Set our event object variables $event->setEventCategory(apply_filters('gform_event_category', $ga_event_data['gaEventCategory'], $form, $entry)); $event->setEventAction(apply_filters('gform_event_action', $ga_event_data['gaEventAction'], $form, $entry)); $event->setEventLabel(apply_filters('gform_event_label', $ga_event_data['gaEventLabel'], $form, $entry)); if ($event_value = apply_filters('gform_event_value', $ga_event_data['gaEventValue'], $form, $entry)) { // Event value must be a valid float! $event_value = GFCommon::to_number($event_value); $event->setEventValue($event_value); } //Push out the event to each UA code foreach ($google_analytics_codes as $ua_code) { $tracking = new \Racecore\GATracking\GATracking($ua_code); $tracking->addTracking($event); try { $tracking->send(); } catch (Exception $e) { error_log($e->getMessage() . ' in ' . get_class($e)); } } // Init tracking object }