wlrp__conversion-tracker

1.1.0 • Public • Published

Conversion Tracking

Initialization

<script src="path/to/conversion.js"></script>
<script>
// Minimal setup
var config = {
		id: 'GTM-123456'
	},
	reporter = new ConversionReporter(config);
</script>

This will inject the GTM script with the ID provided and initialize the Reporter object.

Setting values

// Set manually
reporter.setAmount(10);
reporter.setQuantity(2);

// Or bind to an element that has a `value` property (i.e. input)
reporter.bindAmount(document.getElementById('amountInput'));
reporter.bindQuantity(document.getElementById('quantityInput'));

This will populate the amount and quantity fields of your Reporter object. Reports can now be sent using these values. Add a custom revenue calculator to the config if you want anything more complex.

Sending the conversion

reporter.sendReport();

Conditional tracking

If you need to disable conversion tracking on a per-case basis, you can pass a boolean to the Reporter config's noTrack property. This must be a function returning a boolean value.

function minAmount() {
	// Don't track sales under a certain amount?
	return this.amount < 100;
}

var config = {
		id: 'GTM-123456'
		noTrack: minAmount
	},
	reporter = new ConversionReporter(config);

Default condition:

By default, the reporter will ignore all events if the notrack=1 param is present in the current URL.

function() {
	return window.location.search.indexOf('notrack=1') !== -1;
}

Custom calculator

To use a custom calculator for your revenue reporting, pass a function to the Reporter object's constructor that does the calculations. The function can access the quantity and amount fields of the reporter through the this interface, and must return an integer.

function calculateCustom() {
	// Only 20% commission from partner sales?
	return (this.quantity*this.amount)*0.2;
}

var config = {
		id: 'GTM-123456'
		calculator: calculateCustom
	},
	reporter = new ConversionReporter(config);

Default calculator:

function() {
	return this.quantity * this.amount;
}

Package Sidebar

Install

npm i wlrp__conversion-tracker

Weekly Downloads

0

Version

1.1.0

License

ISC

Unpacked Size

6.22 kB

Total Files

3

Last publish

Collaborators

  • wlrp-kft