I am playing around triggering my goals through the custom code. I have tried using TriggerGoal(long goalId, int value = 0), but seems to be always getting false. I have yet to find the cause of the issue.
I opted to use the other method - TriggerGoal(Guid pageviewGuid, long goalId, int value = 0). But that would mean I need to get the pageviewGuid. Calling _pageviewGuidManager.GetPageviewGuid(); would unfortunately give me null value.
I would appreciate any input. Please see code snippet below:
public class GoalController(IGoalService _goalService,
IAnalyticsPageviewGuidManager _pageviewGuidManager,
IHttpContextAccessor _httpContextAccessor) : UmbracoApiController
{
[HttpPost]
public IActionResult Trigger()
{
HttpContext httpContext = _httpContextAccessor.HttpContext;
var pageviewGuid = _pageviewGuidManager.GetPageviewGuid();
_goalService.TriggerGoal(pageviewGuid.Value, 1, 1);
return Ok();
}
}
Unfortunately it is not possible to trigger a Goal without a valid pageview. By design goals can only be triggered (and are assigned to) a valid pageview. POST requests are not pageviews, only GET requests are/can be. That is there reason why you are not getting a valid pageview GUID here.
One of the ways to resolve this would be to give the Trigger method a GUID parameter, and use the pageview GUID provided by the ‘actual’ pageview (GET Request), that then makes the POST request to this API endpoint.