# Main Implementation

## Implementation of Roivenue Measurement

### Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* **Property ID -** You will need this to set up your script correctly. Contact your account manager to obtain it.

### Initial Setup

1. Download the GTM container [here](https://roipublic.blob.core.windows.net/tagmanager/GTM-RM-container.json).
2. Go to your GTM (at <https://tagmanager.google.com/> )
3. Go to Admin tab select “Import container”, select the container file you have downloaded. Select the workspace you want to import to and set “merge” option - as on the picture bellow.

<figure><img src="https://4279879158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MGvvrlVUvq4TNL0oYP6%2Fuploads%2FaTY9Gv1AE9Q8CE8PLxbD%2Fimage.png?alt=media&#x26;token=a4f32b27-cd9b-4847-9281-a1414b001619" alt=""><figcaption><p>Always set the import to Merge!</p></figcaption></figure>

4. Go to section “Variables”
5. Click on `roivenue.tracking.config` and change the values in the JSON object:
   1. `property_id` - Insert a Property Id provided by your account manager
   2. `api_url` - Should be "<https://tr.roivenue.com>” unless you implement a [custom tracking domain](https://docs.roivenue.com/roivenue-resources/howto/roivenue-measurement-implementation/implementation-through-gtm/tracking-through-own-subdomain). In that case, change the parameter accordingly.
   3. `currency_code` - the default currency code that will be used for tracking purchases used in case that the currency is not specified in purchase object
   4. `event_types` - List of dataLayer events that are automatically being captured when the **measurement tag is triggered.** More about possible changes in the ***Capturing Event*****s** section. Remember that you still need to set the tag triggers!

<figure><img src="https://4279879158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MGvvrlVUvq4TNL0oYP6%2Fuploads%2F7SHu5evZh9qjOeYMm6ni%2Fimage.png?alt=media&#x26;token=216a7a0c-9532-4406-b2cd-f437f4580862" alt=""><figcaption><p>Fill in all the needed information</p></figcaption></figure>

### Setting up Triggers

{% hint style="info" %}
In this step, you are setting up when Roivenue Measurement script should run. By default it runs **with every page view** and with every **Roivenue Measurement Event -** a custom event you can set up only for **Roivenue Measurement.**
{% endhint %}

6. Navigate to Tags section.
7. Select `roivenue.tracking` code.
8. Go to the section “Triggering” and add all triggers when you want Roivenue Measurement script to run

{% hint style="warning" %}
**Important  - This is a frequently missed step**

When a user visits your page for the first time, he is probably presented with a cookie banner. You need to send an event **immediatelly** after they interact with the cookie banner! If you send a next event only after they go to next page, you lose the source of the visit **forever** and as a result you will not know what was the original source of a conversion when they make a purchase in the future.
{% endhint %}

{% hint style="success" %}
The expected additional triggers are those which fire on all of your **conversion events** and also a trigger indicating a **change in consent** from a visitor. (By including this, you can switch between cookie and anonymous tracking within one session, if the consent is collected after the start of that session.)
{% endhint %}

<figure><img src="https://4279879158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MGvvrlVUvq4TNL0oYP6%2Fuploads%2Fpj9IgQAgUfzc5a1kPoFV%2Fimage.png?alt=media&#x26;token=0c45fc36-1896-482c-b4e4-68dd6516b3c0" alt=""><figcaption><p>Add all necessary triggers</p></figcaption></figure>

### Capturing Events

{% hint style="info" %}
When the `roivenue.tracking` script is being executed during the first load, it automatically records a page view event. Then it walks through all objects in `dataLayer.` If the object is an event and the event type is specified in `event_types` (in configuration), the event is automatically captured. Every other time the script is executed, it processes only the new events (according to `event_types`) in the `dataLayer` since the last run.
{% endhint %}

By default these event types are being tracked automatically:

* `page_view`
* `conversion`
* `purchase`
* `ecomm_purchase`

{% hint style="warning" %}
The default configuration works for most businesses. If you need to customize the behavior or change how events parameters are defined, you will find more details in separate pages for [custom pageview tracking](https://docs.roivenue.com/roivenue-resources/howto/roivenue-measurement-implementation/implementation-through-gtm/custom-pageviews-tracking) and custom conversion tracking.&#x20;
{% endhint %}

#### Tracking Page Views <a href="#tracking-page-views" id="tracking-page-views"></a>

{% hint style="info" %}
Page view is tracked automatically when the page is loaded at the first time. The default settings works for majority of cases. If you have a single page application or have problem with a duplication of page views, go to [Custom Pageview Tracking](https://docs.roivenue.com/roivenue-resources/howto/roivenue-measurement-implementation/implementation-through-gtm/custom-pageviews-tracking) article.
{% endhint %}

#### Tracking Users

{% hint style="info" %}
If ‘user’ object is set in any event item in `dataLayer`, we use the `user.id` as the unique user identifier. This is then used for identifying users and connecting their path across multiple devices. Eg:

```
dataLayer.push({'user' : {'id':123}});
```

This can significantly help with reconstruction of customer journeys, especially, if you have a lot of signed users.
{% endhint %}

{% hint style="warning" %}
The User ID should not be a personal sensitive information. For privacy protection reasons it is recommended to use an artificial identifier or a hash.
{% endhint %}

#### Tracking Purchases and Other Conversions

{% hint style="info" %}
If you already have Google Analytics `ecommerce.purchase` event in `dataLayer`, you do not need to implement anything because we will use the following fields to track a **purchase** conversion.

* `value` - the conversion value
* `tax` - the tax
* `currency` - three letter ISO-4217 code of currency
* `transaction_id` - the transaction ID

And we save them into these variables in Roivenue:

* `conversionType`: 'purchase'
* `conversionId`: transaction\_id
* `conversionValue`: value - tax
* `currencyCode`: currency

This is how the event in your `dataLayer` should look like.

{% code lineNumbers="true" %}

```
window.dataLayer.push({
	event: 'purchase',
	ecommerce: {
		transaction_id: "T_12345",
		value: 25.42,
		tax: 4.90,
		shipping: 5.99,
		currency: "USD"
	}
});
```

{% endcode %}
{% endhint %}

In case you would like to track more conversions than just a purchase or you are sending purchases to your `dataLayer` in a format which is different from the example above, you can.

{% hint style="info" %}
It is possible to track any number of conversions by pushing `conversion`event into the `dataLayer`.
{% endhint %}

9. If you want to send additional conversion, these are the parameters you need to send in the conversion event:

* `conversionType` - What type of conversion it is.
* `converisonValue` - the value without tax in specified currency
* `currencyCode` - Three letter ISO-4217 code of currency
* `conversionId` - an optional parameter which we recommend using in case you want to identify unique conversions later.

{% code lineNumbers="true" %}

```
dataLayer.push({
  'event' : 'conversion', 
  'conversionId': 'T_12345', // optional
  'conversionType': 'goal1', 
  'conversionValue' : 123, 
  'currencyCode' : 'CZK'
});
```

{% endcode %}

{% hint style="info" %}
You can add additional fields to the tracked object if you need. Consult this with our support.
{% endhint %}

{% hint style="warning" %}
If you already have some conversion event object in dataLayer, but the event or some of its parameters are named differently, there is an option how to use these in Roivenue Measurement. The recommended way is described in the [Translation Tags](https://docs.roivenue.com/roivenue-resources/howto/roivenue-measurement-implementation/implementation-through-gtm/conversion-tracking) page.
{% endhint %}

### Setting up Consents and Anonymous Tracking

{% hint style="info" %}
Roivenue Anonymous Tracking can be used to track users without using any cookies and to tracks users in a limited scope.
{% endhint %}

{% hint style="warning" %}
By default the Roivenue Measurement is enabled only when consent with analytical cookies has been set via tag manager. If you want to measure in a cookieless mode, you need to modify the tag consent settings.
{% endhint %}

{% hint style="info" %}
If you have your CMP integrated with GTM, you can use Consent Settings on `roivenue.measurement` tag to trigger only when a specific consent has been granted by user. Go to roivenue.measurement tag and specify “Require Additional Consent”.

Alternatively (for example if your CMP is not integrated with GTM consent system) you can edit `roivenue.tracking.enabled` variable implementation. Roivenue measurement will be completely disabled if the variable returns `false`.
{% endhint %}

{% hint style="warning" %}
If you decide to make the tag enabled regardless of GTM consent, set the tag’s Consent Settings / Additional Consent Checks to “Not set” and use consents from any other source.&#x20;
{% endhint %}

<figure><img src="https://4279879158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MGvvrlVUvq4TNL0oYP6%2Fuploads%2FxjztV8fBzcPU5T54qF25%2Fimage.png?alt=media&#x26;token=c0a9a3b1-93dc-48cd-83b8-8c7e3f823887" alt=""><figcaption></figcaption></figure>

By default when Roivenue Measurement tag is triggered, It automatically reads the consent settings from dataLayer and use different persistence settings based on setting of `analytics_storage` consent variable.&#x20;

{% hint style="warning" %}
You should always set up your consent to be pushed to dataLayer as well. It is possible that your CMP updates information in Google Tag Manager that the `analytics_storage` has been granted but does not push this information to dataLayer. In such case, Roivenue Measurement tag will measure but will measure in anonymous mode without cookies. Which can produce less precise results.
{% endhint %}

#### How to set up consent when the Google Tag Manager's consent system is not used

10. Go to Variables.
11. Open the `roivenue.tracking.enabled`
12. Set up the variable in a way that it will return “True” in case the visitor should be tracked (either in a cookie mode or anonymous mode, this will be set up later). In a case you want to completely exclude user from tracking, this variable should return “False”
13. Open the `roivenue.tracking.persistence` variable.
14. Set it up in a way it returns `localStorage+cookie` in case you want to measure a user using cookies. If you want to measure this user in an anonymous mode, this variable should return `sessionStorage`.
15. When a user changes his consent, you should reflect this change in the value returned by `roivenue.tracking.persistence` and potentially also in the `roivenue.tracking.enabled` depending on your implementation.

{% hint style="warning" %}
You are using a custom consent management system and because of that, the steps above are more generic and you need to implement it using outputs from that CMS.
{% endhint %}

{% hint style="warning" %}
As we only act as a data processor, you are responsible for setting up the Roivenue Measurement in line with applicable legislation in your country.
{% endhint %}

#### Cross Domain Tracking in Cookieless Mode

{% hint style="warning" %}
This section is relevant to you only in case your page consists of multiple subdomains. For example: **yourshop.com** for the store and **checkout.yourshop.com** for the checkout.
{% endhint %}

with enabled cookeis, then the events will be connected a single visit. But if user does the same in cookieless mode, there would be a second visit at [checkout.yourshop.com](http://checkout.yourshop.com/) and all conversions then would be tracked with [www.yourshop.com](http://www.yourshop.com/) as a referrer traffic source.

To prevent this:

&#x20;1\.  Set `write_cross_domain_info` in roivenue.measuerement.config to `true`.&#x20;

{% hint style="info" %}
This will put our unique device identifier into **page url**. Our tag modifies the page url by inserting `&rm_cd_cid=xxx-unique-device-id` into url query parameters. In rare case it would conflict with some your query parameter, you can change the parameter name by setting modifying `cross_domain_query_parameter` or setting `cross_domain_use_fragment` to `true`, to put the parameter into url fragment instead.
{% endhint %}

2. A referrer policy needs to be set to allow this mechanism to work.&#x20;

{% hint style="info" %}
You need to either enable passing full referrer information to other domain either globally by including this in page header:

`<meta name="referrer" content="no-referrer-when-downgrade" />`

or by setting the referrer policy for a specific link:

`<a href="https://checkout.yourshop.com" referrerpolicy="no-referrer-when-downgrade">…</a>`

More about referrer policy: [![](https://developer.mozilla.org/favicon-48x48.bc390275e955dacb2e65.png)Referrer-Policy - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy)
{% endhint %}

*Roivenue platform uses PostHog for analytics. PostHog is licensed under the MIT License. For more details, please see* [*PostHog's GitHub repository*](https://github.com/PostHog/posthog) *or the full* [*MIT License text*](https://github.com/PostHog/posthog/blob/master/LICENSE)*.*
