BuilderIO / builder

Drag and drop headless CMS for React, Vue, Svelte, Qwik, and more
https://builder.io
MIT License
6.84k stars 851 forks source link

not able to track abtest impression data in Angular #345

Closed that70schris closed 3 years ago

that70schris commented 3 years ago

tried to follow this:
https://forum.builder.io/t/tracking-builder-data-to-other-analytics-providers/160

but the fields specified in the post are not available on Angular's $event object passed back in the load event:

image
steve8708 commented 3 years ago

thanks for posting @that70schris

one approach I realized you should be able to take for Angular -

// <builder-component (load)="onLoad($event)" ...>...</builder-component>
onLoad(content) {
  const id = content.id;

  // Wait for the cookie to be set 
  setTimeout(() => {
     // Substitute your own cookie reading library/implementation for `readCookie`
    const testId = readCookie(`builder.tests.${id}`);
    if (testId === id) {
       track('someEvent', { testVariationId: testId, name: content.name })
    } else {
      const testVariationInfo = content.variations[testId];
      track('someEvent', { testVariationId: testId, name: testVariationInfo.name })
    }
  })
}
that70schris commented 3 years ago

content doesn't seem to have a variations field

steve8708 commented 3 years ago

ah just took a look at the code and I believe we actually preprocess to find the right match and what you actually just want are the name (which is the test variation name) and id (which is the test variation id) fields - can you give that a try and see if behaving as expected?

that70schris commented 3 years ago

so really just this then?

this.track.impression({
        type: 'builder',
        testVariationId: content.id,
        testVariationName: content.name,
      });
steve8708 commented 3 years ago

I believe so, but you'll want to check and make sure for the same content (e.g. a 50/50 test) you are getting different id/names to ensure I'm correct here

that70schris commented 3 years ago

doesn't look like this is the case, it's reporting the same id/name after 8 page views

steve8708 commented 3 years ago

just to confirm - the tests are sticky with a cookie, did you make sure to test in new incognito browsers (with no other incognito windows open) each time?

steve8708 commented 3 years ago

nvm confirmed my original suspicion is correct. the way we send HTML to angular the test group is not yet known and sending the full test variations object is omited for performance reasons

if it works for you, you'll want to do the cookie method described above, you just won't know the test variation name, but you will have the ID

// <builder-component (load)="onLoad($event)" ...>...</builder-component>
onLoad(content) {
  const id = content.id;

  // Wait for the cookie to be set 
  setTimeout(() => {
     // Substitute your own cookie reading library/implementation for `readCookie`
    const testId = readCookie(`builder.tests.${id}`);
    if (testId === id) {
       track('someEvent', { testVariationId: testId })
    } else {
      track('someEvent', { testVariationId: testId })
    }
  })
}
that70schris commented 3 years ago

the problem is content.variations does not exist

steve8708 commented 3 years ago

correct, you'll see it is not in that code above, you don't need it to know the variation ID

that70schris commented 3 years ago

oh you have const testVariationInfo = content.variations[testId] but you're not using it, got it

steve8708 commented 3 years ago

ah woops missed that :D

that70schris commented 3 years ago

we're all set thanks!