Giter Club home page Giter Club logo

customer-tracker's Introduction

Customer-Tracker

The Customer Tracker records cookies and their respective values and sends them off in an event to Google Analytics. This script assists with the tracking of new and returning customer behaviors and actions. All events can be seen in Google Analytics (Behavior > Events > Top Events).

Getting Started

Installation

Add the latest Customer Tracker script to the header of your site.

<script src="customer-tracker.js"></script>

Setup Google Analytics

Additionally paste the following snippet right after the <head> tag. Make sure to substitute the two filler UA-XXXXXXX-X with your Google Analytics tracking ID.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-X"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-XXXXXXX-X');
</script>

NOTE: For more information about installing the Google Analytics snippet, check out their Quick Start Guide.

Edit Script

After you have installed the required files and Google Analytics script you will need to add the cookie name you would like to track to the script. Open customer-tracker.js and look for the variable customer_id. Replace customer_id with the cookie name you will be tracking.

var cookie_name_1 = "customer_id"; // Insert cookie name here

Tracking Multiple Cookies

Tracking multiple cookies is simple. Go into customer-tracker.js and add the number of cookie_name_X and cookie_value_X variables as cookies that you would like to track. Your code should look something like this:

// Cookies being tracked
var cookie_name_1 = "customer_id";
var cookie_name_2 = "account_type";

// Fetches the value of the cookies
var cookie_value_1 = getCookie(cookie_name_1); 
var cookie_value_2 = getCookie(cookie_name_2); 

Once you've done this find the conditional statment that sends the Google Analytics Events. Inside of the else statement duplicate the event and update the cookie name and cookie value in each instance to reflect the additional cookies you will be tracking. Your code should look something like this:

// Checks if there is a value for the declared cookie
if (!cookie_value_1) {
	ga('send', 'event', 'Customer-Tracker', 'Guest (Logged Out)', 'Guest'); // Sends the value of Guest (Logged Out) to GA
} else {
	ga('send', 'event', 'Customer-Tracker', cookie_name_1, cookie_value_1); // Sends the Cookie Name 1 and Cookie Value 1 to GA
	ga('send', 'event', 'Customer-Tracker', cookie_name_2, cookie_value_2); // Sends the Cookie Name 2 and Cookie Value 2 to GA
}

Customizing the Google Analytics Event

The event that is triggered in customer-tracker.js will by default send the name of the cookie being tracked and its respective value to Google Analytics. You can modify this to send anything you want.

// Default Event
ga('send', 'event', 'Customer-Tracker', cookie_name_1, cookie_value_1);

// Custom Event
ga('send', 'event', 'Customer-Tracker', 'Account Holder', 'True');

Once done publish your changes and enjoy!

Browser Support

Supported Browsers : Chrome, Firefox, Safari, Opera, Edge, IE9+.

License

This source code is licensed under the MIT license.

Developed by Peter Burdette

customer-tracker's People

Contributors

peterburdette avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.