Socket
Book a DemoInstallSign in
Socket

@wizdm/gtag

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wizdm/gtag

Google Tags for Angular

1.0.0
latest
Source
npmnpm
Version published
Maintainers
2
Created
Source

Google Tags (Google Analytics) for Angular

Google Tags (also known as Google Analytics) support for Angular.

Installation

Import the Gtag module in your root module calling the static init() function to setup it up:

import { GtagModule } from '@wizdm/gtag';

@NgModule({
  imports: [
    ...
    GtagModule.init({
      targetId: '<<YOUR GOOGLE ID HERE>>'
    })
  ]
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage Example

The package provides a gtag directive to be used ...:

...
<section fxLayout="column" fxLayoutAlign="center center" gtag="home">

  <p><b>Google Site Tagging</b> (aka Google Analytics) Example</p>

</section>
...

The gtag="home" statement in the .html sends a page-view event to the Google Analytics, Google Ads or Google Marketing Platform account you configured during the initialization.

The “home” text will appear in the report to be the page title while the page location is automatically detected by the directive provided the application makes use of a Router.

Sending Tags Programmatically

The package provides GtagService service implementing all the default Google Analytics events:

import { GtagService } from '@wizdm/gtag';
...
@Component(...)
export class MyComponent()

  constructor(private gtag: GtagService) {

    gtag.login('Google');

    gtag.search('Bologna')
      .then( () => console.log('Search correctly tagged') )
      .catch( e => console.error(e) );

    gtag.addToCart([{
      id: "P12345",
      name: "Android Warhol T-Shirt",
      list_name: "Search Results",
      brand: "Google",
      category: "Apparel/T-Shirts",
      variant: "Black",
      list_position: 1,
      quantity: 2,
      price: '2.0'
    }]);
  }
  ...
}
...

The above example demostrates the way to send a login event (with ‘Google’ as its method) a search event (with ‘Bologna’ as a search term) and a complete add_to_cart e-commerce event.

These methods return a Promise, as the search() call made clear, so to perform asynchronous tasks right after the tagging completed.

Timeouts

In order to prevent the Promise from never resolving, in the eventuality the gtag.js library would fail to load, the package implements a timeout function following Google’s recommendations.

The default timeout value is set to 10s and can be configured during initialization.

Keywords

Angular

FAQs

Package last updated on 05 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.