Skip to content

Commit

Permalink
Add support for custom data layer name (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
auto200 authored Feb 17, 2025
1 parent d279f1f commit 861e000
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# CHANGELOG

- [2.2.0](#220)
- [2.1.0](#210)
- [2.0.1](#201)
- [2.0.0](#200)
- [1.1.0](#110)
- [1.0.0](#100)
- [0.0.1](#001)

## 2.2.0
- add option to configure Data Layer name

## 2.1.0
- update @piwikpro/tracking-base-library version
- add cross-domain-tracking service
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { NgxPiwikProModule } from "@piwikpro/ngx-piwik-pro";
declarations: [AppComponent],
imports: [
BrowserModule,
NgxPiwikProModule.forRoot("container-id", "container-url", "nonce-hash"),
NgxPiwikProModule.forRoot("container-id", "container-url", {nonce: "nonce-hash"}),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
],
providers: [],
Expand Down Expand Up @@ -144,6 +144,24 @@ import { NgxPiwikProModule, NgxPiwikProRouterModule } from '@piwikpro/ngx-piwik-
export class AppModule {}
```

##### Customize Data Layer name

```ts
@Component(...)
import { NgxPiwikProModule, NgxPiwikProRouterModule } from '@piwikpro/ngx-piwik-pro';
...

@NgModule({
...
imports: [
...
NgxPiwikProModule.forRoot('container-id', 'container-url', {dataLayerName: "myDataLayerName"}),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
]
})
export class AppModule {}
```

### Piwik PRO Services

#### Send Custom Events
Expand Down
20 changes: 19 additions & 1 deletion docs/README_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { NgxPiwikProModule } from "@piwikpro/ngx-piwik-pro";
declarations: [AppComponent],
imports: [
BrowserModule,
NgxPiwikProModule.forRoot("container-id", "container-url", "nonce-hash"),
NgxPiwikProModule.forRoot("container-id", "container-url", {nonce: "nonce-hash"}),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
],
providers: [],
Expand Down Expand Up @@ -138,6 +138,24 @@ import { NgxPiwikProModule, NgxPiwikProRouterModule } from '@piwikpro/ngx-piwik-
export class AppModule {}
```

#### Customize Data Layer name

```ts
@Component(...)
import { NgxPiwikProModule, NgxPiwikProRouterModule } from '@piwikpro/ngx-piwik-pro';
...

@NgModule({
...
imports: [
...
NgxPiwikProModule.forRoot('container-id', 'container-url', {dataLayerName: "myDataLayerName"}),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
]
})
export class AppModule {}
```

## Piwik PRO Services

### Send Custom Events
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@piwikpro/ngx-piwik-pro-project",
"version": "2.0.1",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-piwik-pro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@piwikpro/ngx-piwik-pro",
"version": "2.1.0",
"version": "2.2.0",
"description": "Piwik PRO tracking library for Angular apps.",
"keywords": [
"PiwikPRO",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { APP_INITIALIZER, FactoryProvider, PLATFORM_ID, isDevMode } from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import PiwikPro, { DataLayer } from '@piwikpro/tracking-base-library';

import { NGX_PIWIK_PRO_SETTINGS_TOKEN } from '../tokens/ngx-piwik-pro-settings.token';
import PiwikPro from '@piwikpro/tracking-base-library';
import { PiwikProSettings } from '../interfaces/piwik-pro-settings.interface';

export const NGX_PIWIK_PRO_INITIALIZER_PROVIDER: FactoryProvider = {
Expand Down Expand Up @@ -51,6 +51,10 @@ export function PiwikProInitializer(
}
}

if(settings.dataLayerName){
DataLayer.setDataLayerName(settings.dataLayerName)
}

const s: HTMLScriptElement = document.createElement('script');
s.async = true;
if (settings.nonce) {
Expand All @@ -60,6 +64,7 @@ export function PiwikProInitializer(
containerId: settings.containerId,
containerUrl: settings.containerURL,
nonceValue: settings.nonce,
dataLayerName: settings.dataLayerName
})

const head: HTMLHeadElement = document.getElementsByTagName('head')[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface PiwikProSettings {
import { InitOptions } from '@piwikpro/tracking-base-library';

export type PiwikProSettings = {
containerId: string;
containerURL: string;
nonce?: string;
}
} & InitOptions;
30 changes: 20 additions & 10 deletions projects/ngx-piwik-pro/src/lib/ngx-piwik-pro.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { InitOptions } from '@piwikpro/tracking-base-library';

import { NGX_PIWIK_PRO_SETTINGS_TOKEN } from './tokens/ngx-piwik-pro-settings.token';
import { PiwikProSettings } from './interfaces/piwik-pro-settings.interface';
import { NGX_PIWIK_PRO_INITIALIZER_PROVIDER } from './initializers/piwik-pro.initializer';
Expand All @@ -10,17 +12,25 @@ declare global {
}

@NgModule({
declarations: [
],
imports: [
],
exports: [
]
declarations: [],
imports: [],
exports: [],
})
export class NgxPiwikProModule {
constructor() {}

static forRoot(containerId: string, containerURL: string, nonce?: string): ModuleWithProviders<NgxPiwikProModule> {
static forRoot(
containerId: string,
containerURL: string,
nonceOrOptions?: string | InitOptions
): ModuleWithProviders<NgxPiwikProModule> {
const options =
typeof nonceOrOptions === 'string'
? {
nonce: nonceOrOptions,
}
: nonceOrOptions;

return {
ngModule: NgxPiwikProModule,
providers: [
Expand All @@ -29,11 +39,11 @@ export class NgxPiwikProModule {
useValue: {
containerId,
containerURL,
nonce
} as PiwikProSettings
...options,
} as PiwikProSettings,
},
NGX_PIWIK_PRO_INITIALIZER_PROVIDER,
],
}
};
}
}
6 changes: 4 additions & 2 deletions projects/piwik-pro-angular-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { DataLayerService } from '@piwik-pro/ngx-piwik-pro/src/lib/services/data-layer/data-layer.service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
title = 'piwik-pro-angular-demo';
constructor(private titleService: Title) {
constructor(private titleService: Title, private readonly dataLayerService: DataLayerService) {
this.titleService.setTitle('Home Page')
this.dataLayerService.push({ name: "my custom data layer entry" })
}
}
13 changes: 11 additions & 2 deletions projects/piwik-pro-angular-demo/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { CustomEventsService } from '@piwik-pro/ngx-piwik-pro/src/lib/services/c
import { MatIconModule } from '@angular/material/icon';
import { MatDividerModule } from '@angular/material/divider';
import { DownloadAndOutlinkComponent } from './pages/download-and-outlink/download-and-outlink.component';
import { DataLayerService } from '@piwik-pro/ngx-piwik-pro/src/lib/services/data-layer/data-layer.service';

@NgModule({
declarations: [
Expand All @@ -41,7 +42,15 @@ import { DownloadAndOutlinkComponent } from './pages/download-and-outlink/downlo
AppRoutingModule,
NavbarModule,
EventListModule,
NgxPiwikProModule.forRoot(environment.containerId, environment.containerURL),
NgxPiwikProModule.forRoot(
environment.containerId,
environment.containerURL,
// optional config
{
nonce: '_nonce_', // nonce string
dataLayerName: 'myDataLayerName' // custom data layer name
}
),
NgxPiwikProRouterModule.forRoot(),
BrowserAnimationsModule,
MatCardModule,
Expand All @@ -54,7 +63,7 @@ import { DownloadAndOutlinkComponent } from './pages/download-and-outlink/downlo
UserManagementService,
ContentTrackingService,
CustomEventsService,

DataLayerService
],
bootstrap: [AppComponent]
})
Expand Down

0 comments on commit 861e000

Please sign in to comment.