Expand my Community achievements bar.

SOLVED

Trying to re-use data layer from GTM in DTM...HELP!

Avatar

Level 2

I have a site that we're moving from GTM/GA to Launch/AA. We are attempting to use some of the events and variables from the current data layer to avoid new development work; however, I'm struggling to reference the data layer successfully in Launch.

Below is an example of what I'm working with. In GTM, I would simply set up a custom event for the "virtualPageView" event and capture the "virtualUrl" as a variable. I'm sure this is doable in Launch as well, but I'm struggling to figure out how to reference these elements and use the event as a rule to fire my pageviews.

1548790_pastedImage_0.png

Here is how I'm trying to format my event/rule in Launch. What am I doing wrong?? Is it the element? If so, what should it be? Should I be referencing the event like "dataLayer.virtualPageView"? Any help is appreciated. Thanks.

1548791_pastedImage_1.png

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Putting both suggestions together (Mine and Tomas's),  here's that intercept code (monkey patch):

if (window.dataLayer) {

  if (!window.dataLayer.__oldPush) {

    window.dataLayer.__oldPush = window.dataLayer.push;

    window.dataLayer.push = function(payload) {

      if (payload.event && typeof payload.event === "string") {

        _satellite.track(payload.event, payload);

      }

      window.dataLayer.__oldPush(payload);

    }

  }

}

This would go after the GTM header code.  Note that this code expects a single object to be passed to the push function.  It is possible to code this so that multiple objects can be passed, but it's pretty uncommon to do a dataLayer.push with multiple objects.

You will need to use "Direct Call" as the rule trigger instead of "Custom Event".  The GTM event string will be your Launch Direct Call Identifier.

TAG TEAM FTW!

tagteam.gif

View solution in original post

6 Replies

Avatar

Community Advisor

Your idea is a good one, but I'm pretty sure that the GTM data layer doesn't emit events that Launch could listen for.

You could monkey patch the push method of the dataLayer object and add a custom event dispatcher.

Avatar

Level 9

If i interpret your code correctly, your dataLayer object is just an array, where you push events into?

Then you would need to configure your dataLayer with a very simple JSON Schema in the Context Hub Extension. Something along the line of this:

{

    "$id": "MyDataLayer",

    "type": "array",

    "$schema": "http://json-schema.org/draft-06/schema#",

    "items": {

        "$id": "MyEvents",

        "title": "Empty Object",

        "description": "This accepts anything, as long as it's valid JSON."

    }

}

Though usually it has to be an object as the root element...

Then you can create a DataElement pointing to this datalayer and in the Event configuration select "Data Element Change" which then would get fired every time you push something into this array.

A more robust way would be to intercept this push and fire a Direct Call rule where the pushed object is the payload:

_satellite.track('new-event', eventData);

Avatar

Correct answer by
Community Advisor

Putting both suggestions together (Mine and Tomas's),  here's that intercept code (monkey patch):

if (window.dataLayer) {

  if (!window.dataLayer.__oldPush) {

    window.dataLayer.__oldPush = window.dataLayer.push;

    window.dataLayer.push = function(payload) {

      if (payload.event && typeof payload.event === "string") {

        _satellite.track(payload.event, payload);

      }

      window.dataLayer.__oldPush(payload);

    }

  }

}

This would go after the GTM header code.  Note that this code expects a single object to be passed to the push function.  It is possible to code this so that multiple objects can be passed, but it's pretty uncommon to do a dataLayer.push with multiple objects.

You will need to use "Direct Call" as the rule trigger instead of "Custom Event".  The GTM event string will be your Launch Direct Call Identifier.

TAG TEAM FTW!

tagteam.gif

Avatar

Level 2

Thanks, this is super helpful. Not being a developer, I'm still confused as to how I would set this up to receive the data being sent into the dataLayer. For example, in my screenshot above, how would I define the schema for the "virtualPageView" event?

Avatar

Community Advisor

As an example, we'll create a rule triggered by a Core: Direct Call event with an Actions: AA Set Variable (Page Name) and AA Send Beacon.  The end result will look something like this:

Screen Shot 2018-08-28 at 11.18.54 AM.png

We start by setting the event that will trigger the rule using the Core extension's "Direct Call" event type (see below).

For "Identifier", enter the string that matches the value 'event' from the GTM data layer.

In your example, window.dataLayer[2].event is set to "virtualPageView".

Screen Shot 2018-08-28 at 11.06.31 AM.png

Then in the rule's conditions, exceptions, and actions, you can make reference to the event payload using the syntax, %event.detail.xxxxxxx% where xxxxxxx is the object key that you wish to use.

In our example, we'll set the Adobe Analytic pageName to the virtualUrl value from your GTM event using %event.detail.virtualUrl% as shown below:

Screen Shot 2018-08-28 at 11.12.28 AM.png

Now save your rule, publish the changes to Development, and test away.

LMK if this gets you going,

-Stew

Avatar

Level 1

Hi!

Thank you so much for the very well explained solution, is helping us a lot . Now, we would like to know if there's any way to store the dataLayer content using the Adobe Context Datahub. It seems that the JSON schema that we are using is not working, could you show us a working schema that collects for example this GTM dataLayer structure?

[

  {

    "event": "login",

    "userLoginState": "logged",

    "userId": "XXXXXXX",

    "userGender": "male",

    "userAge": 25,

    "userType": "customer"

  },

  {

    "event": "funnelStep",

    "level 1": "cat",

    "level 2": "dog",

    "level 3": "horse"

  },

  {

    "event": "question",

    "process": "buy",

    "question": "generic request",

    "answer": "no"

  },

]

We''ve tried to insert this schema:

{

  "$id": "http://example.com/root.json",

  "type": "array",

  "$schema": "http://json-schema.org/draft-04/schema#",

  "required": [

    "userLoginState",

    "userId",

    "userGender",

    "userAge",

    "userType",

    "event",

    "level 1",

    "level 2",

    "level 3",

    "process",

    "question",

    "answer"

  ],

  "properties": {

    "event": {

      "type": "string"

    },

    "level 1": {

      "type": "string"

    },

    "level 2": {

      "type": "string"

    },

    "level 3": {

      "type": "string"

    },

    "process": {

      "type": "string"

    },

    "userId": {

      "type": "string"

    },

    "question": {

      "type": "string"

    },

    "userAge": {

      "type": "integer"

    },

    "answer": {

      "type": "string"

    },

    "userType": {

      "type": "string"

    },

    "userGender": {

      "type": "string"

    },

    "userLoginState": {

      "type": "string"

    }

  },

  "definitions": {}

}

But when we create a Data Element and try to get the value of any field we only get an "undefined" as response . Thanks for your time reading this post!