How to Integrate Segment Data Analytics into WordPress? — codementor.tech

chirag patel
4 min readJan 9, 2019
https://www.codementor.tech/how-to-integrate-segment-data-analytics-into-wordpress/

In this article, We are going to explain “HOW YOU CAN INTEGRATE SEGMENT DATA ANALYTICS TO YOUR WORDPRESS SITE ?

First, Let me explain you

WHAT IS SEGMENT?

  • A segment is a single platform that collects, stores, and routes your user data to hundreds of tools with the flick of a switch.
  • So basically you can spend more time using your data and less time to track it down through a segment.
  • For more information about a segment, you can go through THIS link.

Analytics, marketing automation, email, user testing, error reporting, and support tools all require the same data: who are your users and what are they doing.

Without Segment, you have to instrument each tool separately, even though you send them all the same data. Segment eliminates this extra code and replaces the tool installation process.

You just send your data to us, then we translate and route it to all of your tools.

Use the Segment API to track user events like page views, clicks, and sign-ups on your mobile app and website. Next switch on marketing, product, and analytics tools you want to use in Segment’s control panel. Then Segment:

  • Collects and stores your data in one place
  • Prepares and translates the data into a format each tool understands
  • Sends the data to your designated tools, so you never have to install tracking again.

HOW YOU CAN INTEGRATE IT TO YOUR WEBSITE?

The first thing you need to make an account on segment website. (IT’S FREE :-P).

Then you can simply follow the instruction as given and after that, you just need to login into it.

After successful login, you need to create a workspace where you can simply add source. (As shown below).

https://www.codementor.tech/how-to-integrate-segment-data-analytics-into-wordpress/
https://www.codementor.tech/how-to-integrate-segment-data-analytics-into-wordpress/

In my case, I have already added one source but in your case, you need to add a source.

Simply clicks on “Add Source”. Then you will see the next screen.(As shown below).

https://www.codementor.tech/how-to-integrate-segment-data-analytics-into-wordpress/

You can select any sources for example “JavaScript”, “IOS”… But in my case, I have so many complexities and some custom functionalities as well so i have chosen “PHP” as a source.

If you can’t find “PHP” in a source list. Then just simply You can see a filter source section to the left top side. Just type “PHP”.

After selecting PHP, Just need to click on connect (As shown below).

https://www.codementor.tech/how-to-integrate-segment-data-analytics-into-wordpress/

After, Clicks on connect you will see the source setup screen (As shown below).

After, Simply clicks on “Add Source”.

Now, You need to download the library, clone the repository from GitHub into your desired application directory. or You can simply download the zip file and extract it to your application directory.

Here is the link where you can download the library or clone the repo.
https://github.com/segmentio/analytics-php

After, You need to require the Segment.php file into your functions.php file.
require_once("/path/to/analytics-php/lib/Segment.php");

Now, you’re ready to actually initialize the module.

class_alias('Segment', 'Analytics');
Segment::init("YOUR_WRITE_KEY");

How you can find your source write key? let me show you how ? (as shown below).

Click on Source -> Settings -> API’s ->WRITE KEY .

Now, the key point “Identify”.

The Method identify is how you tell Segment who the current user is. It includes a unique User ID and any optional traits that you might know about them.

Here’s what a basic call to identify might look like:

Segment::identify(array(
"userId" => "17",
"traits" => array(
"name" => "Chirag Patel",
"email" => "chiragpatel@programmingschool.io"
)
));

Now, Track Action.

The track method is how you tell Segment about which actions your users are performing on your site. Every action triggers what we call an “event”, which can also have associated “properties”.

Here’s what a call to track might look like when a user signs up:

Segment::track(array(
"userId" => "17",
"event" => "Logged in",
"properties" => array(
"Name" => "chirag",
"Surname" => "Patel",
"Email" => "chiragpatel@programmingschool.io"
)
));

So, As shown above, is the small overview of the segment and how you can integrate it into your WordPress website or any CRM.

You can check live events in “debugger” section.

Hope you like the article please share and cheers.

Happy Coding!

--

--