aM-Vee aM-Vee - 1 month ago 7x
Javascript Question

Implement Google Analytics With Userid in Open Cart

i want to track every customer how could i do it the documentation on google analytics have this code

ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.

how do i pass userid in this as open cart have an admin panel setup to enter google analytics and ehat if my customer is not logged in what should i do.


Opencart uses a setting in administration where you insert your whole GA code. You can use it as well for user tracking. The only change you'd do is to add this line of JS code into that field in administration:

ga('set', '&uid', #CUSTOMER_ID#);

This string will be then parsed by PHP in catalog/controller/common/header.php like this - find this line

$this->data['google_analytics'] = html_entity_decode( ... );

and after this line put this code:

if ($this->customer->isLogged()) {
    // replace %s with customer ID
    $this->data['google_analytics'] = str_replace('#CUSTOMER_ID#', $this->customer->getId(), $this->data['google_analytics']);
} else {
    // customer is not logged in, remove the user tracking part
    $this->data['google_analytics'] = str_replace("ga('set', '&uid', #CUSTOMER_ID#);", "", $this->data['google_analytics']);

This should do it - also with the check if customer is logged in.