npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-tag-commander

v2.2.1

Published

Integrate CommandersAct's tag container with your React applications seamlessly using the `react-tag-commander` wrapper.

Downloads

4,817

Readme

react-tag-commander

Integrate CommandersAct's tag container with your React applications seamlessly using the react-tag-commander wrapper.

Table of Contents

Features

  • Automatic page tracking
  • Event triggering
  • Supports multiple containers

Installation and Quick Start

Installation

  1. Using NPM:

    npm i react-tag-commander
  2. Direct Include: Fetch dist/index.es5.min.js or index.es6.min.js and include it in your project.

    <script src="react-tag-commander/dist/index.es5.min.js"></script>

Import

  1. For ES6:

    import TC_Wrapper from 'react-tag-commander';
  2. For ES5:

     const TC_Wrapper = require('react-tag-commander');
  3. Direct Include:

    const TC_Wrapper = window.TC_Wrapper;

Setup

  1. Initialize your Data Layer: Set up your data layer early in your web application, preferably in a <script> block in the head.

    tc_vars = [];
  2. Add a Container: You can either include your container with a <script> tag or utilize the addContainer method from the wrapper.

  • For the latter, be aware it's asynchronous. Ensure your application renders asynchronously too.
import React from "react";
import TC_Wrapper from "react-tag-commander";

function App() {
    const [tcReady, setTcReady] = useState(false);
    
    useEffect(() => {
        const wrapper = TC_Wrapper.getInstance();
        Promise.all([
            wrapper.addContainer('container_head', '/tag-commander-head.js', 'head'), 
            wrapper.addContainer('container_body', '/tag-commander-body.js', 'body')
        ]).then(() => {
            setIsReady(true);
        });
    }, []);
    
    return ( tcReady ? <div>Containers loaded</div> : <div>Now loading</div> );
}

Methods

Many methods are asynchronous. If you want to ensure that a method has been executed before continuing, you can use the await keyword. Please check the function definition to see if it is asynchronous.

Container Management

// Adding a container
await wrapper.addContainer('my-custom-id', '/url/to/container.js', 'head');

// Removing a container
wrapper.removeContainer('my-custom-id');

Variable Management

// Set variables
await wrapper.setTcVars({ env_template : "shop", ... });

// Update a single variable
await wrapper.setTcVar('env_template', 'super_shop');

// Get a variable
const myVar = wrapper.getTcVar('VarKey');

// Remove a variable
wrapper.removeTcVar('VarKey');

Events

  • Refer to the base documentation on events for an understanding of events in general.
  • The method "triggerEvent" is the new name of the old method "captureEvent"; an alias has been added to ensure backward compatibility.
// Triggering an event
// eventLabel: Name of the event as defined in the container
// htmlElement: Calling context. Usually the HTML element on which the event is triggered, but it can be the component.
// data: event variables
await wrapper.triggerEvent(eventLabel, htmlElement, data);

Reloading Containers

Manual Reload

Update your container after any variable change.

await wrapper.reloadContainer(siteId, containerId, options);

Exclusions

You can state an exclusion array to your options object like below.

const options = {
        exclusions: [
            'datastorage',
            'deduplication',
            'internalvars',
            'privacy'
        ]
    };
await wrapper.reloadContainer(siteId, containerId, options);

Please see the container's documentation for other options.

On Route Change

Utilize the trackPageLoad function for updating on route changes.

function SampleView() {
  
  /* States and other effects */
  
  useEffect(() => {
    const wrapper = TC_Wrapper.getInstance();
    wrapper.trackPageLoad({ tcVars: { page: 'home' }})
  }, []);

  /* Render & other custom code */
}

Server-side Rendering (SSR)

react-tag-commander works seamlessly with frameworks utilizing Server-side Rendering (SSR) (for example Next.js). However, the wrapper is interacting with the DOM objects document and window, which are not available on the server. Therefore, you have to make sure that wrapper methods are only executed on the client-side. This can be achieved by using hooks like useEffect, useCallback or useState or, executing it in a callback function that doesn't run on the server, for example the submit function of a form.

Examples:

// Throws an 'window is not defined' error, as the code is executed on the server and trackPageLoad interacts with the window object.
function SampleView() {
    const wrapper = TC_Wrapper.getInstance();
    wrapper.trackPageLoad({tcVars: {page: 'home'}})
}
// Works as the code is executed on the client only
function SampleView() {
    useEffect(() => {
        const wrapper = TC_Wrapper.getInstance();
        wrapper.trackPageLoad({tcVars: {page: 'home'}})
    }, []);
}

Another option is to check whether window is defined before executing a method.

function SampleView() {
    if (typeof window !== 'undefined') {
        // client-side-only code
        const wrapper = TC_Wrapper.getInstance();
        wrapper.trackPageLoad({tcVars: {page: 'home'}})
    }
}

Sample App

To help you with your implementation we provided a sample application. To run it clone the repo then run:

cd tag-commander-sample-app
yarn start

Then, visit http://localhost:3000.

Development

After forking, set up your environment:

npm install

Commands available:

gulp

Contribute

To contribute to this project, please read the CONTRIBUTE.md file.

License

This module uses the MIT License. Contributions are welcome.