Coder Social home page Coder Social logo

google-tag-manager's Introduction

Google Tag Manager

Data Layer generator

Installation

composer require zdenekgebauer/google-tag-manager

Usage

Simple event

$event = new EventShare();
$event->method = 'Twitter';
$event->contentType = 'image';
$event->itemId = 'C_12345';

echo ((new DataLayer($event))->render());  // window.dataLayer.push({"event":"share","content_type":"image","item_id":"C_12345"});

Ecommerce event with items

$event = new EventPurchase();
$event->transactionId = '1234';
$event->value = '9.99';
$event->currency = 'USD';

$item = new Item('SKU12', 'Some gadget', 9.99, 1);
$item->brand = 'Acme';
$item->category = 'Cool Gadgets';
$event->addItem($item);

echo ((new DataLayer($event))->render());

Custom content

class MyDataLayerContent extends DataLayerContent
{
    public ?string $requiredProperty = null;
    public ?string $optionalProperty = null;
    
    public function __construct()
    {
        $this->requiredProperties = ['requiredProperty'];
    }
        
    /**
     * @throws Exception
     */
    public function assertValid(): void
    {
        parent::assertValid(); // check required properties
        // .... some additional check        
    }
    
    public function jsonSerialize(): stdClass
    {
        $result = new stdClass();
        $result->required_property = $this->requiredProperty;
        Utils::addProperty($result, 'optional', $this->optionalProperty);
        return $result;
    }
}

$content = new MyDataLayerContent();
$content->requiredProperty = 'some';
echo ((new DataLayer($content))->render());  // window.dataLayer.push({"required_property":"some"});

Official documentation

[Google Analytics 4 events] https://developers.google.com/analytics/devguides/collection/ga4/reference/events

[Google Analytics 4 events for ecommerce] https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtm

Licence

Released under the WTFPL license http://www.wtfpl.net/about/.

google-tag-manager's People

Contributors

zdenekgebauer avatar

Watchers

James Cloos avatar  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.