In this guide we are going to provide you with step-by-step instructions on how to setup Google Analytics in Magento 2 on a CentOS 7 VPS. Google Analytics is a great web analytics service that tracks and reports website traffic. In order to enable Google Analytics in Magento 2, you need to install Magento 2 on your VPS, create a new Google Analytics Account or log in to your existing account if you have one, obtain a tracking ID and configure the Magento 2 website to use the newly created tracking ID.
1. Update OS packages
Before starting with the installation procedure, update the server OS packages with the latest available packages by running the following commands:
sudo yum clean all sudo yum update
2. Install PHP 7
CentOS 7 has PHP 5.4 installed by default, but PHP 5.4 is not compatible with Magento 2. We can remove PHP 5.4 and install PHP 7.1 version. To do so, check which PHP 5.4 packages are installed on the server and remove them:
sudo rpm -qa | grep php php-cli-5.4.16-42.el7.x86_64 php-pdo-5.4.16-42.el7.x86_64 php-common-5.4.16-42.el7.x86_64 php-5.4.16-42.el7.x86_64 sudo yum remove php php-cli php-common php-pdo
Install PHP 7.1 using the iuscommunity repo:
sudo rpm -Uvh https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-15.ius.centos7.noarch.rpm sudo yum update sudo yum install php71u php71u-devel php71u-bcmath php71u-gd php71u-pdo php71u-mysqlnd php71u-xml php71u-mcrypt php71u-intl php71u-mbstring php71u-json php71u-iconv php71u-opcache php71u-imap php71u-soap
3. Create a new database for Magento 2
mysql -u root -p MariaDB [(none)]> CREATE DATABASE magento2; MariaDB [(none)]>GRANT ALL PRIVILEGES ON magento2.* TO 'magento2'@'localhost' IDENTIFIED BY 'Y0urPa55w0rd'; MariaDB [(none)]>flush privileges; MariaDB [(none)]>quit;
4. Create a new virtual host in Apache:
vi /etc/httpd/conf.d/magento2.conf
Add the following lines and save the file:
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/magento2/
<Directory /var/www/html/magento2>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
ErrorLog /var/www/html/magento2/logs/yourdomain.com_error.log
CustomLog /var/www/html/magento2/logs/yourdomain.com_access.log combined
</VirtualHost>
Set proper permissions to the files and directories of Magento 2:
sudo chown -R apache:apache /var/www/html/
Edit the PHP configuration file and add/modify the following settings (change the timezone according to your actual timezone):
memory_limit = 512M date.timezone = US/Chicago
Restart the Apache service for the changes to take effect:
sudo systemctl restart httpd
5. Install Magento 2
Download the latest version of Magento 2, and extract it on the server:
cd /opt/ wget https://github.com/magento/magento2/archive/2.2.1.zip unzip 2.*.zip mv magento2* /var/www/html/magento2/
Download and install composer using:
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
Install Magento 2 using the following commands:
cd /var/www/html/magento2/ sudo composer install
The above command will install Magento 2 on your server by installing required frameworks.
Open http://yourdomain.com in your favorite web browser and follow the easy instructions. For security reason, once the installation is complete, do not forget to remove write permissions from the ‘/var/www/html/magento2/app/etc’ directory.
Login to your Magento 2 back-end, go to to Stores >> Configuration >> Sales >> Google API, expand the Google Analytics option, select ‘Yes’ to enable it, paste the tracking code in the ‘Account Number’ field and click on the ‘Save Config’ button for the changes to take effect.
Log in to your Google Analytics account, and after a couple of minutes you should be able to track the activity on your website.
Of course you don’t have to set up Google Analytics on Magento 2 if you use one of our Optimized CentOS 7 VPS Hosting services, in which case you can simply ask our expert Linux admins to add the Google Analytics code to Magento 2 for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post, on How to set up Google Analytics on Magento 2, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.